KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > core > product > SplashInfo


1 /*******************************************************************************
2  * Copyright (c) 2005, 2007 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.pde.internal.core.product;
12
13 import java.io.PrintWriter JavaDoc;
14 import java.util.StringTokenizer JavaDoc;
15
16 import org.eclipse.pde.internal.core.iproduct.IProductModel;
17 import org.eclipse.pde.internal.core.iproduct.ISplashInfo;
18 import org.w3c.dom.Element JavaDoc;
19 import org.w3c.dom.Node JavaDoc;
20
21 public class SplashInfo extends ProductObject implements ISplashInfo {
22
23     public static final int F_DEFAULT_BAR_X_OFFSET = 5;
24     public static final int F_DEFAULT_BAR_Y_OFFSET = 275;
25     public static final int F_DEFAULT_BAR_WIDTH = 445;
26     public static final int F_DEFAULT_BAR_HEIGHT = 15;
27
28     public static final int F_DEFAULT_MESSAGE_X_OFFSET = 7;
29     public static final int F_DEFAULT_MESSAGE_Y_OFFSET = 252;
30     public static final int F_DEFAULT_MESSAGE_WIDTH = 445;
31     public static final int F_DEFAULT_MESSAGE_HEIGHT = 20;
32     
33     private static final char[] VALID_HEX_CHARS = new char[] {
34         '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
35         'a', 'b', 'c', 'd', 'e', 'f',
36         'A', 'B', 'C', 'D', 'E', 'F'
37     };
38     private static final long serialVersionUID = 1L;
39     private String JavaDoc fLocation;
40     private boolean fCustomizeProgressBar;
41     private int[] fProgressGeometry;
42     private boolean fCustomizeProgressMessage;
43     private int[] fMessageGeometry;
44     private boolean fCustomizeForegroundColor;
45     private String JavaDoc fForegroundColor;
46     
47     private String JavaDoc fFieldSplashHandlerType;
48
49     public SplashInfo(IProductModel model) {
50         super(model);
51     }
52
53     public void setLocation(String JavaDoc location, boolean blockNotification) {
54         String JavaDoc old = fLocation;
55         fLocation = location;
56         if (!blockNotification && isEditable())
57             firePropertyChanged(P_LOCATION, old, fLocation);
58     }
59
60     public String JavaDoc getLocation() {
61         return fLocation;
62     }
63
64     public void parse(Node JavaDoc node) {
65         if (node.getNodeType() == Node.ELEMENT_NODE) {
66             Element JavaDoc element = (Element JavaDoc)node;
67             setLocation(element.getAttribute(P_LOCATION), true);
68             setProgressGeometry(getGeometryArray(element.getAttribute(P_PROGRESS_GEOMETRY)), true);
69             setMessageGeometry(getGeometryArray(element.getAttribute(P_MESSAGE_GEOMETRY)), true);
70             setForegroundColor(element.getAttribute(P_FOREGROUND_COLOR), true);
71             // Parse the splash handler type
72
setFieldSplashHandlerType(element.getAttribute(F_ATTRIBUTE_HANDLER_TYPE), true);
73         }
74     }
75
76     public void write(String JavaDoc indent, PrintWriter JavaDoc writer) {
77         if (!hasData())
78             return;
79         
80         writer.print(indent + "<splash"); //$NON-NLS-1$
81

82         if (fLocation != null && fLocation.length() > 0)
83             writeProperty(indent, writer, P_LOCATION, getWritableString(fLocation));
84         
85         String JavaDoc progres = getGeometryString(fProgressGeometry);
86         if (fCustomizeProgressBar && progres != null)
87             writeProperty(indent, writer, P_PROGRESS_GEOMETRY, getWritableString(progres));
88         
89         String JavaDoc message = getGeometryString(fMessageGeometry);
90         if (fCustomizeProgressMessage && message != null)
91             writeProperty(indent, writer, P_MESSAGE_GEOMETRY, getWritableString(message));
92         
93         if (fCustomizeForegroundColor && isValidHexValue(fForegroundColor))
94             writeProperty(indent, writer, P_FOREGROUND_COLOR, getWritableString(fForegroundColor));
95         
96         // Write the splash handler type if it is defined
97
if (isDefinedSplashHandlerType()) {
98             writeProperty(indent, writer, F_ATTRIBUTE_HANDLER_TYPE,
99                     fFieldSplashHandlerType);
100         }
101         
102         writer.print(" />"); //$NON-NLS-1$
103
}
104
105     private void writeProperty(String JavaDoc indent, PrintWriter JavaDoc writer, String JavaDoc name, String JavaDoc value) {
106         writer.println();
107         writer.print(indent + indent + name + "=\"" + value + "\""); //$NON-NLS-1$ //$NON-NLS-2$
108
}
109     
110     public void setProgressGeometry(int[] geo, boolean blockNotification) {
111         fCustomizeProgressBar = geo != null;
112         int[] old = fProgressGeometry;
113         fProgressGeometry = geo;
114         if (!blockNotification && isEditable())
115             firePropertyChanged(P_PROGRESS_GEOMETRY, old, fProgressGeometry);
116     }
117
118     public int[] getProgressGeometry() {
119         return fCustomizeProgressBar ? fProgressGeometry : null;
120     }
121     
122     public void setMessageGeometry(int[] geo, boolean blockNotification) {
123         fCustomizeProgressMessage = geo != null;
124         int[] old = fMessageGeometry;
125         fMessageGeometry = geo;
126         if (!blockNotification && isEditable())
127             firePropertyChanged(P_MESSAGE_GEOMETRY, old, fMessageGeometry);
128     }
129
130     public int[] getMessageGeometry() {
131         return fCustomizeProgressMessage ? fMessageGeometry : null;
132     }
133
134     public void setForegroundColor(String JavaDoc hexColor, boolean blockNotification) throws IllegalArgumentException JavaDoc {
135         if (hexColor != null && hexColor.length() == 0)
136             hexColor = null;
137         if (hexColor != null && !isValidHexValue(hexColor))
138             throw new IllegalArgumentException JavaDoc();
139         fCustomizeForegroundColor = hexColor != null;
140         String JavaDoc old = fForegroundColor;
141         fForegroundColor = hexColor;
142         if (!blockNotification && isEditable())
143             firePropertyChanged(P_FOREGROUND_COLOR, old, fForegroundColor);
144     }
145
146     public String JavaDoc getForegroundColor() {
147         return fCustomizeForegroundColor ? fForegroundColor : null;
148     }
149     
150     public static String JavaDoc getGeometryString(int[] geometry) {
151         if (geometry == null || geometry.length < 4)
152             return null;
153         return Integer.toString(geometry[0]) + "," + //$NON-NLS-1$
154
Integer.toString(geometry[1]) + "," + //$NON-NLS-1$
155
Integer.toString(geometry[2]) + "," + //$NON-NLS-1$
156
Integer.toString(geometry[3]);
157     }
158     
159     public static int[] getGeometryArray(String JavaDoc tokenizedValue) {
160         if (tokenizedValue == null || tokenizedValue.length() == 0)
161             return null;
162         
163         StringTokenizer JavaDoc tokenizer = new StringTokenizer JavaDoc(tokenizedValue, ","); //$NON-NLS-1$
164
int position = 0;
165         int[] geo = new int[4];
166         while (tokenizer.hasMoreTokens())
167             geo[position++] = Integer.parseInt(tokenizer.nextToken());
168         return geo;
169     }
170     
171     private boolean isValidHexValue(String JavaDoc value) {
172         if (value == null || value.length() != 6)
173             return false;
174         for (int i = 0; i < value.length(); i++) {
175             boolean found = false;
176             for (int j = 0; j < VALID_HEX_CHARS.length; j++) {
177                 if (value.charAt(i) == VALID_HEX_CHARS[j]) {
178                     found = true;
179                     break;
180                 }
181             }
182             if (!found)
183                 return false;
184         }
185         return true;
186     }
187     
188     private boolean hasData() {
189         return (fLocation != null && fLocation.length() > 0) ||
190                 (fCustomizeForegroundColor && fForegroundColor != null && isValidHexValue(fForegroundColor)) ||
191                 isDefinedGeometry() ||
192                 isDefinedSplashHandlerType();
193     }
194
195     /**
196      * @return
197      */

198     public boolean isDefinedSplashHandlerType() {
199         if ((fFieldSplashHandlerType != null) &&
200                 (fFieldSplashHandlerType.length() > 0)) {
201             return true;
202         }
203         return false;
204     }
205     
206     public void addProgressBar(boolean add, boolean blockNotification) {
207         boolean old = fCustomizeProgressBar;
208         fCustomizeProgressBar = add;
209         int[] geo = getProgressGeometry();
210         if (add)
211             setProgressGeometry(geo != null ? geo : new int[] {
212                     F_DEFAULT_BAR_X_OFFSET,
213                     F_DEFAULT_BAR_Y_OFFSET,
214                     F_DEFAULT_BAR_WIDTH,
215                     F_DEFAULT_BAR_HEIGHT},
216                     blockNotification);
217          else if (!blockNotification && isEditable())
218             firePropertyChanged("", Boolean.toString(old), Boolean.toString(add)); //$NON-NLS-1$
219
}
220
221     public void addProgressMessage(boolean add, boolean blockNotification) {
222         boolean mold = fCustomizeProgressMessage;
223         boolean cold = fCustomizeForegroundColor;
224         fCustomizeProgressMessage = add;
225         fCustomizeForegroundColor = add;
226         int[] geo = getMessageGeometry();
227         String JavaDoc foreground = getForegroundColor();
228         if (add) {
229             setMessageGeometry(geo != null ? geo : new int[] {
230                     F_DEFAULT_MESSAGE_X_OFFSET,
231                     F_DEFAULT_MESSAGE_Y_OFFSET,
232                     F_DEFAULT_MESSAGE_WIDTH,
233                     F_DEFAULT_MESSAGE_HEIGHT},
234                     blockNotification);
235             setForegroundColor(foreground != null ? foreground : "000000", blockNotification); //$NON-NLS-1$
236
} else if (!blockNotification && isEditable())
237             firePropertyChanged("", Boolean.toString(mold || cold), Boolean.toString(add)); //$NON-NLS-1$
238
}
239
240     /* (non-Javadoc)
241      * @see org.eclipse.pde.internal.core.iproduct.ISplashInfo#getFieldSplashHandlerType()
242      */

243     public String JavaDoc getFieldSplashHandlerType() {
244         return fFieldSplashHandlerType;
245     }
246
247     /* (non-Javadoc)
248      * @see org.eclipse.pde.internal.core.iproduct.ISplashInfo#setFieldSplashHandlerType(java.lang.String)
249      */

250     public void setFieldSplashHandlerType(String JavaDoc type, boolean blockNotification) {
251         String JavaDoc old = fFieldSplashHandlerType;
252         fFieldSplashHandlerType = type;
253         if ((blockNotification == false) &&
254                 isEditable()) {
255             firePropertyChanged(F_ATTRIBUTE_HANDLER_TYPE, old, fFieldSplashHandlerType);
256         }
257     }
258
259     /* (non-Javadoc)
260      * @see org.eclipse.pde.internal.core.iproduct.ISplashInfo#isDefinedGeometry()
261      */

262     public boolean isDefinedGeometry() {
263         if ((fCustomizeProgressBar && fProgressGeometry != null) ||
264                 (fCustomizeProgressMessage && fMessageGeometry != null)) {
265             return true;
266         }
267         return false;
268     }
269 }
270
Popular Tags