source: other-projects/gs3-webservices-java-client/trunk/src/GS3DemoClient/org/greenstone/gs3client/ColourCombo.java@ 26174

Last change on this file since 26174 was 15222, checked in by ak19, 16 years ago

Greenstone3 web services demo-clientadded to GS3's other-projects

File size: 6.9 KB
Line 
1package org.greenstone.gs3client;
2
3import java.awt.Color;
4import java.awt.Component;
5import javax.swing.JTextField;
6
7
8/**
9 * Background and selection color combinations for the Java-Client's
10 * interface. Stores Greenstone, Fedora and default colour combinations
11 * and has methods for converting colours of items.
12 * @author ak19
13*/
14public class ColourCombo {
15 /** Interface ColourChangeable defines one method, changeUIColour(),
16 * which classes whose user-interface colours can change may
17 * implement. They can then choose to call either of ColourCombo's
18 * changeColor() methods or changeAncestorColor() if necessary.
19 * Though it's not compulsory to implement this interface, it may
20 * help in grouping the classes that are ColourChangeable. */
21 public interface ColourChangeable {
22 /** Subclasses define this method in order to change the
23 * colour of the interface */
24 public void changeUIColour();
25 }
26
27 /* Some fixed colours to be used in the user interface of GS3JavaClient */
28 /** Greenstone transparent background colour. Useful for trees' text
29 * backgrounds which otherwise default to white even when the tree
30 * background is not white. Alpha value=0 for transparency */
31 static final Color transparent = new Color(0, 0, 0, 0);
32 /** Greenstone dark background colour */
33 static final Color green = new Color(176, 208, 176);
34 /** Greenstone light background colour */
35 static final Color lightGreen = new Color(224, 240, 224);
36 /** Fedora dark background colour */
37 static final Color blue = new Color(128, 180, 216);
38 /** Fedora light background colour */
39 static final Color lightBlue = new Color(218, 237, 252);
40 /** Selection colour for Fedora and Greenstone */
41 static final Color yellow = new Color(240, 240, 140);
42
43 /* Some default colours (system greys and selection colors) */
44 /** Default background colour */
45 static final Color grey = new Color(238, 238, 238);
46 /** Fixed background colour for textareas, editorPanes (html) */
47 static final Color background = Color.white;
48
49 /** background colours for panels, labels */
50 public final Color dark;
51 /** background colours for comboboxes, lists, textfields, textareas */
52 public final Color light;
53
54 /* The fixed colour combination settings for Greenstone, Fedora and default */
55 /** System default greys */
56 public static final ColourCombo defaultColors
57 = new ColourCombo(grey, grey);
58 /** Greenstone greens */
59 public static final ColourCombo greenstoneColors
60 = new ColourCombo(green, lightGreen);
61 /** Fedora's colour combinations of blue */
62 public static final ColourCombo fedoraColors
63 = new ColourCombo(blue, lightBlue);
64 /** The colour combination currently being used in the interface */
65 protected static ColourCombo current = defaultColors;
66
67 /** Constructor to create a ColourCombo combination of colours
68 * @param dark is the colour to set JPanels to.
69 * @param light is the colour to set comboboxes, lists and non-editable
70 * textfields to.
71 */
72 public ColourCombo(Color dark, Color light)
73 {
74 this.dark = dark;
75 this.light = light;
76 }
77
78 // access methods
79 public static ColourCombo getCurrentCombo() { return current; }
80 public static void setCurrentCombo(ColourCombo c) { current = c; }
81
82 /** Sets the active colour combination based on the value of DL.
83 * @param DL represents the current digital library. Values can be
84 * GS3JavaClient.GREENSTONE, GS3JavaClient.FEDORA, GS3JavaClient.SELECT.
85 * Anything else and it defaults to GS3JavaClient.SELECT as well.
86 */
87 public static void setColour(int DL) {
88 switch(DL) {
89 case GS3JavaClient.GREENSTONE:
90 setCurrentCombo(greenstoneColors);
91 break;
92
93 case GS3JavaClient.FEDORA:
94 setCurrentCombo(fedoraColors);
95 break;
96
97 case GS3JavaClient.SELECT:
98 default:
99 setCurrentCombo(defaultColors);
100 break;
101 }
102 }
103
104 /**
105 * @param c is the Component for which it is to be determined whether it
106 * should be a lighter or darker colour.
107 * @return true if Component c should be a lighter colour (returns true
108 * for non-editable JTextFields)
109 */
110 protected static boolean isLightColor(Component c) {
111 // JTextAreas and JEditorPane left white
112
113 // Only JComboBoxes, JLists (and non-editable TextFields) are turned to
114 // the light colour
115 if(c.getClass().getName().equalsIgnoreCase("javax.swing.JComboBox")) {
116 return true;
117 }
118 if(c.getClass().getName().equalsIgnoreCase("javax.swing.JList")) {
119 return true;
120 }
121 // NO LONGER TRUE: Although we can set the the tree pane background to
122 // a light colour, we are unable to get the CellRenderer to do anything
123 // upon selection so leave this white=the colour of selections
124 // INSTEAD: since we have now set the textbackground of treenodes to
125 // transparent, we can colour the background of tree displays.
126 if(c.getClass().getName().toLowerCase().contains("tree")) {
127 return true;
128 }
129 return false;
130 }
131
132 /** Given an array of components, sets their background colours to a
133 * dark or light colour setting as appropriate.
134 * @param leafComponents is the array of components whose background
135 * colours are to change.
136 */
137 public static void changeColor(Component[] leafComponents)
138 {
139 for(int i = 0; i < leafComponents.length; i++) {
140 changeColor(leafComponents[i]);
141 }
142 }
143
144 /** Given a Component, sets its background colours to a dark or
145 * light colour setting as appropriate.
146 * @param c is the component whose background colours are to change.
147 */
148 public static void changeColor(Component c)
149 {
150 // NOT NEEDED: since we made text background of treenodes transparent!
151 // skip all trees, leave them with their default (white) colouring
152 //if(c.getClass().getName().toLowerCase().contains("tree")) {
153 //return;
154 //}
155
156 // deal with JTextFields next, based on whether they are editable
157 // or not
158 if(c.getClass().getName().equalsIgnoreCase("javax.swing.JTextField")) {
159 JTextField field = (JTextField)c;
160 if(field.isEditable()) {
161 field.setBackground(background); // default white
162 } else { // must be set to light (non-editable) colour
163 field.setBackground(current.light); // default white
164 }
165 } else if(isLightColor(c)) {
166 // for JLists, JComboBoxes, JTextFields need
167 // to set light background color
168 c.setBackground(current.light);
169 } else { // need to set background color to dark
170 c.setBackground(current.dark);
171 }
172 }
173
174 /** Given a component, sets its ancestors to the dark colour setting.
175 * @param c is the component whose ancestors' background colours are
176 * to be set.
177 */
178 public static void changeAncestorColor(Component c) {
179 // assume c's background color has been changed
180 do {
181 c = c.getParent(); // change parent color
182 // Leave tabbed panes grey, but change the colour of all other
183 // containers - all ancestor components will be containers of
184 // some kind (labels or panels)
185 if(!c.getClass().getName().equalsIgnoreCase("javax.swing.JTabbedPane"))
186 {
187 c.setBackground(current.dark);
188 }
189 } while(c.getParent() != null); // cycle through all ancestors
190 }
191}
Note: See TracBrowser for help on using the repository browser.