001 // This file is part of the program FRYSK. 002 // 003 // Copyright 2006, Red Hat Inc. 004 // 005 // FRYSK is free software; you can redistribute it and/or modify it 006 // under the terms of the GNU General Public License as published by 007 // the Free Software Foundation; version 2 of the License. 008 // 009 // FRYSK is distributed in the hope that it will be useful, but 010 // WITHOUT ANY WARRANTY; without even the implied warranty of 011 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 012 // General Public License for more details. 013 // 014 // You should have received a copy of the GNU General Public License 015 // along with FRYSK; if not, write to the Free Software Foundation, 016 // Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. 017 // 018 // In addition, as a special exception, Red Hat, Inc. gives You the 019 // additional right to link the code of FRYSK with code not covered 020 // under the GNU General Public License ("Non-GPL Code") and to 021 // distribute linked combinations including the two, subject to the 022 // limitations in this paragraph. Non-GPL Code permitted under this 023 // exception must only link to the code of FRYSK through those well 024 // defined interfaces identified in the file named EXCEPTION found in 025 // the source code files (the "Approved Interfaces"). The files of 026 // Non-GPL Code may instantiate templates or use macros or inline 027 // functions from the Approved Interfaces without causing the 028 // resulting work to be covered by the GNU General Public 029 // License. Only Red Hat, Inc. may make changes or additions to the 030 // list of Approved Interfaces. You must obey the GNU General Public 031 // License in all respects for all of the FRYSK code and other code 032 // used in conjunction with FRYSK except the Non-GPL Code covered by 033 // this exception. If you modify this file, you may extend this 034 // exception to your version of the file, but you are not obligated to 035 // do so. If you do not wish to provide this exception without 036 // modification, you must delete this exception statement from your 037 // version and license this file solely under the GPL without 038 // exception. 039 040 package frysk.gui.prefs; 041 042 import java.util.Iterator; 043 import java.util.LinkedList; 044 import java.util.prefs.Preferences; 045 046 import org.gnu.gdk.Color; 047 048 /** 049 * ColorPreference models a color-values preference for Frysk 050 * 051 */ 052 public class ColorPreference extends FryskPreference { 053 054 public interface ColorPreferenceListener{ 055 void preferenceChanged(String prefName, Color newColor); 056 } 057 058 protected Color currentColor; 059 protected Color fallback; 060 061 protected LinkedList listeners; 062 063 /** 064 * Creates a new ColorPreference 065 * @param name The name of the preference 066 * @param fallback The default color to use 067 */ 068 public ColorPreference(String name, Color fallback) { 069 this.name = name; 070 this.fallback = fallback; 071 this.listeners = new LinkedList(); 072 } 073 074 /** 075 * Sets the current color for this preference. Note that this is 076 * not saved into the model until {@see #save(Preferences)} is called. 077 * @param currentColor The new color 078 */ 079 public void setCurrentColor(Color currentColor) { 080 this.currentColor = currentColor; 081 } 082 083 /** 084 * 085 * @return The current value of this preference 086 */ 087 public Color getCurrentColor() { 088 return this.currentColor; 089 } 090 091 /** 092 * Saves the value of this preference into the preference model and 093 * notify all attached listeners 094 */ 095 public void save(Preferences prefs) { 096 this.model.putInt(name + "_R", this.currentColor.getRed()); 097 this.model.putInt(name + "_G", this.currentColor.getGreen()); 098 this.model.putInt(name + "_B", this.currentColor.getBlue()); 099 100 Iterator it = this.listeners.iterator(); 101 while(it.hasNext()) 102 ((ColorPreferenceListener) it.next()).preferenceChanged(this.name, this.currentColor); 103 } 104 105 /** 106 * Sets the preference to use the provided model and loads the values from 107 * it. 108 */ 109 public void load(Preferences prefs) { 110 this.model = prefs; 111 112 this.revert(); 113 } 114 115 /** 116 * Adds a listener to this preference that will get notified whenever 117 * the value of this preference changes 118 * @param listener The object to notify when the preference changes. 119 */ 120 public void addListener(ColorPreferenceListener listener){ 121 this.listeners.add(listener); 122 listener.preferenceChanged(this.name, this.currentColor); 123 } 124 125 /** 126 * Restores the current value of this preference from the model. 127 */ 128 public void revert() { 129 int r = model.getInt(name + "_R", fallback.getRed()); 130 int g = model.getInt(name + "_G", fallback.getGreen()); 131 int b = model.getInt(name + "_B", fallback.getBlue()); 132 this.currentColor = new Color(r, g, b); 133 } 134 }