Revision 20779 trunk/applications/appgvSIG/src/com/iver/cit/gvsig/gui/panels/ColorChooserPanel.java

View differences:

ColorChooserPanel.java
88 88
import javax.swing.JCheckBox;
89 89
import javax.swing.JColorChooser;
90 90
import javax.swing.JFrame;
91
import javax.swing.JLabel;
91 92
import javax.swing.JPanel;
92 93
import javax.swing.JSlider;
93 94
import javax.swing.SwingUtilities;
......
118 119
    private Dimension dim2 = new Dimension(dim.width, dim.height*2+5);
119 120
    private ArrayList<ActionListener> actionListeners = new ArrayList<ActionListener>();
120 121
    private boolean withTransp;
121
    private boolean withNoFill;
122
    private boolean withTranspPerc;
123
	private boolean withNoFill;
122 124
    private JCheckBox chkUseColor;
123 125
	private JSlider sldTransparency;
124
    private boolean muteSldTransparency = false;
126
	private JLabel lblTransparency;
127
	private boolean muteSldTransparency = false;
128
	private double perc = 0.392156863 ;/*100/255*/
125 129
	private ChangeListener sldAction = new ChangeListener() {
126 130
		public void stateChanged(ChangeEvent e) {
127 131
			if (!muteSldTransparency) {
128 132
				int alphaValue = sldTransparency.getValue();
129 133
				setAlpha(alphaValue);
134
				
135
				if(withTranspPerc) {
136
					int percValue = (int) (alphaValue * perc);
137
					lblTransparency.setText(" " + String.valueOf(percValue));
138
				}
130 139
				fireActionPerformed();
131 140
			}
132 141
		}
133 142

  
134 143
	};
135 144

  
145
	
136 146
    public ColorChooserPanel() {
137 147
        this(false);
138 148
    }
139 149

  
150
    /**
151
	 * Constructor method
152
	 * 
153
	 * @param withTransparencySlider boolean that specifies if the user
154
	 * wants a slider with the color chooser panel to control the transparency
155
	 * of the selected color.Also, it will appear a text which specifies the transparency percentage.
156
	 * @param withNoFill boolean that specifies if the color chooser panel
157
	 * allows a null value for the color selected (true case). If this values 
158
	 * is false, when the color selected is null then black color is assigned
159
	 * automatically. 
160
	 */
140 161
    public ColorChooserPanel(boolean withTransparencySlider) {
141 162
    	this(withTransparencySlider,false);
142 163
    	
......
156 177
	public ColorChooserPanel(boolean withTransparencySlider, boolean withNoFill) {
157 178

  
158 179
		this.withTransp=withTransparencySlider;
180
		this.withTranspPerc=withTransparencySlider;
159 181
		this.withNoFill=withNoFill;
160 182

  
161 183
		try {
......
165 187
				int width = withNoFill ? dim2.width - 40 : dim2.width;
166 188
				sldTransparency.setPreferredSize(new Dimension(width, dim2.height/2));
167 189
				sldTransparency.setToolTipText(PluginServices.getText(this, "transparencia"));
190

  
191
				if(withTranspPerc) {	
192
					lblTransparency = new JLabel();
193
					int percValue = (int) (sldTransparency.getValue() * perc);
194
					lblTransparency.setText(" " + String.valueOf(percValue));
195
					lblTransparency.setPreferredSize(new Dimension(30,20));
196
				}
197

  
198
				sldTransparency.setValue(255);
199

  
168 200
			}
169 201
			jbInit();
170 202
			colorPanel.setLineColor(null);
......
205 237

  
206 238
		if(withNoFill) {
207 239
			chkUseColor = new JCheckBox();
240
			chkUseColor.setSelected(true);
208 241
			c.fill = GridBagConstraints.HORIZONTAL;
209 242
			c.gridwidth = 1;
210 243
			c.gridx = 0;
......
220 253
		}
221 254

  
222 255

  
223

  
224 256
		if (withTransp) {
225 257

  
226 258
			c.fill = GridBagConstraints.HORIZONTAL;
......
228 260
			c.gridx = 1;
229 261
			c.gridy = 1;
230 262
			pane.add(sldTransparency,c);
263

  
264

  
265
			if(withTranspPerc) {
266
				c.fill = GridBagConstraints.HORIZONTAL;
267
				c.gridwidth = 3;
268
				c.gridx = 4;
269
				c.gridy = 1;
270
				pane.add(lblTransparency,c);
271
			}
272

  
231 273
		}
232 274

  
233 275
		add(pane);
......
270 312
	 * @param e Action Event
271 313
	 */
272 314
	private void useColor_actionPerformed(ActionEvent e) {
315
		if(chkUseColor.isSelected())
316
			setColor(color);
317
		else setColor(null);
273 318
		fireActionPerformed();
274 319
	}
275 320

  
321
     public void addActionListener(ActionListener l) {
322
        actionListeners.add(l);
323
    }
324

  
325
    public void removeActionListener(ActionListener l) {
326
        actionListeners.remove(l);
327
    }
328

  
329
    protected void fireActionPerformed() {
330
        for (Iterator<ActionListener> i = actionListeners.iterator(); i.hasNext();) {
331
            i.next().actionPerformed(new ActionEvent(this, 0, null));
332
        }
333
    }
276 334
	/**
277 335
	 * Sets the color of the chooser panel.
278 336
	 *  
......
280 338
	 */
281 339
	public void setColor(Color color) {
282 340

  
283
		this.color = color;
284
		if( color != null)
341
		if( color != null) {
342
			this.color = color;
285 343
			setAlpha(color.getAlpha());
344
			updateColorPanel();
345
		}
286 346

  
287
		updateColorPanel();
288

  
289 347
	}
290 348
    
291 349
	/**
......
303 361
		if (withTransp) {
304 362
			sldTransparency.setValue(alpha);
305 363
			sldTransparency.validate();
364
			int percValue = (int) (alpha * perc);
365
			lblTransparency.setText(" " + String.valueOf(percValue));
306 366
		}
367
	
307 368
		updateColorPanel();
308 369
		muteSldTransparency = false;
370
		fireActionPerformed();
309 371
	}
310 372
	
311 373
	public int getAlpha() {
......
322 384
        colorPanel.repaint();
323 385
    }
324 386

  
325
    public void addActionListener(ActionListener l) {
326
        actionListeners.add(l);
327
    }
328

  
329
    public void removeActionListener(ActionListener l) {
330
        actionListeners.remove(l);
331
    }
332

  
333
    protected void fireActionPerformed() {
334
        for (Iterator<ActionListener> i = actionListeners.iterator(); i.hasNext();) {
335
            i.next().actionPerformed(new ActionEvent(this, 0, null));
336
        }
337
    }
338

  
387
    
339 388
	/**
340 389
	 * Obtains the selected color in the color chooser panel. 
341 390
	 * 
......
359 408
		if(withTransp) {
360 409
			sldTransparency.setEnabled(newEnabled);
361 410
		}
411
		if(withTranspPerc) {
412
			lblTransparency.setEnabled(newEnabled);
413
		}
362 414
		if(withNoFill) {
363 415
			chkUseColor.setEnabled(newEnabled);
364 416
		}

Also available in: Unified diff