Revision 42472

View differences:

trunk/org.gvsig.desktop/org.gvsig.desktop.library/org.gvsig.ui/src/main/java/org/gvsig/app/gui/panels/ColorChooserPanel.java
3 3
 *
4 4
 * Copyright (C) 2007-2013 gvSIG Association.
5 5
 *
6
 * This program is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU General Public License
8
 * as published by the Free Software Foundation; either version 3
9
 * of the License, or (at your option) any later version.
6
 * This program is free software; you can redistribute it and/or modify it under
7
 * the terms of the GNU General Public License as published by the Free Software
8
 * Foundation; either version 3 of the License, or (at your option) any later
9
 * version.
10 10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
11
 * This program is distributed in the hope that it will be useful, but WITHOUT
12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
14
 * details.
15 15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19
 * MA  02110-1301, USA.
16
 * You should have received a copy of the GNU General Public License along with
17
 * this program; if not, write to the Free Software Foundation, Inc., 51
18
 * Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 19
 *
21
 * For any additional information, do not hesitate to contact us
22
 * at info AT gvsig.com, or visit our website www.gvsig.com.
20
 * For any additional information, do not hesitate to contact us at info AT
21
 * gvsig.com, or visit our website www.gvsig.com.
23 22
 */
24 23

  
25 24
/*
......
119 118
import javax.swing.event.ChangeListener;
120 119

  
121 120
import org.gvsig.i18n.Messages;
121
import org.slf4j.Logger;
122
import org.slf4j.LoggerFactory;
122 123

  
123

  
124

  
125 124
/**
126 125
 * A custom Colour Scheme Browser - custom choices based on JColorChooser.
127 126
 */
128

  
129 127
public class ColorChooserPanel extends JPanel {
130
	private GridBagLayout gridBagLayout1 = new GridBagLayout();
131
	private JButton changeButton = new JButton();
132 128

  
129
    private static final Logger logger = LoggerFactory.getLogger(ColorChooserPanel.class);
130
    
131
    private GridBagLayout gridBagLayout1 = new GridBagLayout();
132
    private JButton changeButton = new JButton();
133 133

  
134 134
    //{DEFECT-PREVENTION} In accessors, it's better to use "workbench = newWorkbench"
135 135
    //rather than "this.workbench = workbench", because otherwise if you misspell the
......
139 139
    private Color color = Color.black;
140 140
//    private int alpha;
141 141
    private Dimension dim = new Dimension(100, 22);
142
    private Dimension dim2 = new Dimension(dim.width, dim.height*2+5);
142
    private Dimension dim2 = new Dimension(dim.width, dim.height * 2 + 5);
143 143
    private ArrayList<ActionListener> actionListeners = new ArrayList<ActionListener>();
144 144
    private boolean withTransp;
145 145
    private boolean withTranspPerc;
146
	private boolean withNoFill;
146
    private boolean withNoFill;
147 147
    private JCheckBox chkUseColor;
148
	private JSlider sldTransparency;
149
	private JLabel lblTransparency;
150
	private boolean muteSldTransparency = false;
151
	private double perc = 0.392156863 ;/*100/255*/
152
	private ChangeListener sldAction = new ChangeListener() {
153
		public void stateChanged(ChangeEvent e) {
154
			if (!muteSldTransparency) {
155
				int alphaValue = sldTransparency.getValue();
156
				setAlpha(alphaValue);
148
    private JSlider sldTransparency;
149
    private JLabel lblTransparency;
150
    private boolean muteSldTransparency = false;
151
    private double perc = 0.392156863;/*100/255*/
157 152

  
158
				if(withTranspPerc) {
159
					int percValue = (int) (alphaValue * perc);
160
					lblTransparency.setText( String.valueOf(percValue) + "%");
161
				}
162
				fireActionPerformed();
163
			}
164
		}
153
    private ChangeListener sldAction = new ChangeListener() {
154
        public void stateChanged(ChangeEvent e) {
155
            if (!muteSldTransparency) {
156
                int alphaValue = sldTransparency.getValue();
157
                setAlpha(alphaValue);
165 158

  
166
	};
159
                if (withTranspPerc) {
160
                    int percValue = (int) (alphaValue * perc);
161
                    lblTransparency.setText(String.valueOf(percValue) + "%");
162
                }
163
                fireActionPerformed();
164
            }
165
        }
167 166

  
167
    };
168 168

  
169 169
    public ColorChooserPanel() {
170 170
        this(false);
171 171
    }
172 172

  
173 173
    /**
174
	 * Constructor method
175
	 *
176
	 * @param withTransparencySlider boolean that specifies if the user
177
	 * wants a slider with the color chooser panel to control the transparency
178
	 * of the selected color.Also, it will appear a text which specifies the transparency percentage.
179
	 * @param withNoFill boolean that specifies if the color chooser panel
180
	 * allows a null value for the color selected (true case). If this values
181
	 * is false, when the color selected is null then black color is assigned
182
	 * automatically.
183
	 */
174
     * Constructor method
175
     *
176
     * @param withTransparencySlider boolean that specifies if the user wants a
177
     * slider with the color chooser panel to control the transparency of the
178
     * selected color.Also, it will appear a text which specifies the
179
     * transparency percentage.
180
     * @param withNoFill boolean that specifies if the color chooser panel
181
     * allows a null value for the color selected (true case). If this values is
182
     * false, when the color selected is null then black color is assigned
183
     * automatically.
184
     */
184 185
    public ColorChooserPanel(boolean withTransparencySlider) {
185
    	this(withTransparencySlider,false);
186
        this(withTransparencySlider, false);
186 187

  
187 188
    }
188 189

  
189
	/**
190
	 * Constructor method
191
	 *
192
	 * @param withTransparencySlider boolean that specifies if the user
193
	 * wants a slider with the color chooser panel to control the transparency
194
	 * of the selected color.
195
	 * @param withNoFill boolean that specifies if the color chooser panel
196
	 * allows a null value for the color selected (true case). If this values
197
	 * is false, when the color selected is null then black color is assigned
198
	 * automatically.
199
	 */
200
	public ColorChooserPanel(boolean withTransparencySlider, boolean withNoFill) {
190
    /**
191
     * Constructor method
192
     *
193
     * @param withTransparencySlider boolean that specifies if the user wants a
194
     * slider with the color chooser panel to control the transparency of the
195
     * selected color.
196
     * @param withNoFill boolean that specifies if the color chooser panel
197
     * allows a null value for the color selected (true case). If this values is
198
     * false, when the color selected is null then black color is assigned
199
     * automatically.
200
     */
201
    public ColorChooserPanel(boolean withTransparencySlider, boolean withNoFill) {
201 202

  
202
		this.withTransp=withTransparencySlider;
203
		this.withTranspPerc=withTransparencySlider;
204
		this.withNoFill=withNoFill;
203
        this.withTransp = withTransparencySlider;
204
        this.withTranspPerc = withTransparencySlider;
205
        this.withNoFill = withNoFill;
205 206

  
206
		try {
207
			if (withTransp) {
208
				sldTransparency = new JSlider(0, 255);
209
				sldTransparency.addChangeListener(sldAction);
210
				int width = withNoFill ? dim2.width - 40 : dim2.width;
211
				sldTransparency.setPreferredSize(new Dimension(width-5, dim2.height/2));
212
				sldTransparency.setToolTipText(Messages.getText("transparencia"));
207
        try {
208
            if (withTransp) {
209
                sldTransparency = new JSlider(0, 255);
210
                sldTransparency.addChangeListener(sldAction);
211
                int width = withNoFill ? dim2.width - 40 : dim2.width;
212
                sldTransparency.setPreferredSize(new Dimension(width - 5, dim2.height / 2));
213
                sldTransparency.setToolTipText(Messages.getText("transparencia"));
213 214

  
214
				if(withTranspPerc) {
215
					lblTransparency = new JLabel();
216
					int percValue = (int) (sldTransparency.getValue() * perc);
217
					lblTransparency.setText(String.valueOf(percValue) +"%");
218
					lblTransparency.setPreferredSize(new Dimension(40,20));
219
				}
215
                if (withTranspPerc) {
216
                    lblTransparency = new JLabel();
217
                    int percValue = (int) (sldTransparency.getValue() * perc);
218
                    lblTransparency.setText(String.valueOf(percValue) + "%");
219
                    lblTransparency.setPreferredSize(new Dimension(40, 20));
220
                }
220 221

  
221
				sldTransparency.setValue(255);
222
                sldTransparency.setValue(255);
222 223

  
223
			}
224
			jbInit();
225
			colorPanel.setLineColor(null);
226
			changeButton.setToolTipText(Messages.getText("browse"));
224
            }
225
            jbInit();
226
            colorPanel.setLineColor(null);
227
            changeButton.setToolTipText(Messages.getText("browse"));
227 228

  
228
		} catch (Exception e) {
229
			e.printStackTrace();
230
		}
231
	}
229
        } catch (Exception e) {
230
            logger.warn("Can't create ColorChooserPanel",e);
231
        }
232
    }
232 233

  
233
	private void jbInit() throws Exception {
234
		JPanel pane = new JPanel(new GridBagLayout());
235
		GridBagConstraints c = new GridBagConstraints();
234
    private void jbInit() throws Exception {
235
        JPanel pane = new JPanel(new GridBagLayout());
236
        GridBagConstraints c = new GridBagConstraints();
236 237

  
237
		changeButton.setText("...");
238
		changeButton.addActionListener(new java.awt.event.ActionListener() {
239
			public void actionPerformed(ActionEvent e) {
240
				changeButton_actionPerformed(e);
241
			}
242
		});
243
		outerColorPanel.setBorder(BorderFactory.createLoweredBevelBorder());
244
		outerColorPanel.setPreferredSize(new Dimension(dim.width/2, dim.height));
245
		outerColorPanel.setBackground(Color.white);
238
        changeButton.setText("...");
239
        changeButton.addActionListener(new java.awt.event.ActionListener() {
240
            public void actionPerformed(ActionEvent e) {
241
                changeButton_actionPerformed(e);
242
            }
243
        });
244
        outerColorPanel.setBorder(BorderFactory.createLoweredBevelBorder());
245
        outerColorPanel.setPreferredSize(new Dimension(dim.width / 2, dim.height));
246
        outerColorPanel.setBackground(Color.white);
246 247

  
248
        c.fill = GridBagConstraints.HORIZONTAL;
249
        c.gridx = 1;
250
        c.gridy = 0;
251
        c.gridwidth = 2;
252
        pane.add(outerColorPanel, c);
247 253

  
248
		c.fill = GridBagConstraints.HORIZONTAL;
249
		c.gridx = 1;
250
		c.gridy = 0;
251
		c.gridwidth = 2;
252
		pane.add(outerColorPanel,c);
254
        c.fill = GridBagConstraints.HORIZONTAL;
255
        c.gridx = 3;
256
        c.gridy = 0;
257
        c.gridwidth = 1;
258
        pane.add(changeButton, c);
259
        outerColorPanel.add(colorPanel);
253 260

  
254
		c.fill = GridBagConstraints.HORIZONTAL;
255
		c.gridx = 3;
256
		c.gridy = 0;
257
		c.gridwidth = 1;
258
		pane.add(changeButton,c);
259
		outerColorPanel.add(colorPanel);
261
        if (withNoFill) {
262
            chkUseColor = new JCheckBox();
263
            chkUseColor.setSelected(true);
264
            c.fill = GridBagConstraints.HORIZONTAL;
265
            c.gridwidth = 1;
266
            c.gridx = 0;
267
            c.gridy = 1;
260 268

  
261
		if(withNoFill) {
262
			chkUseColor = new JCheckBox();
263
			chkUseColor.setSelected(true);
264
			c.fill = GridBagConstraints.HORIZONTAL;
265
			c.gridwidth = 1;
266
			c.gridx = 0;
267
			c.gridy = 1;
269
            chkUseColor.addActionListener(new java.awt.event.ActionListener() {
270
                public void actionPerformed(ActionEvent e) {
271
                    useColor_actionPerformed(e);
272
                }
273
            });
274
            pane.add(chkUseColor, c);
275
        }
268 276

  
277
        if (withTransp) {
269 278

  
270
			chkUseColor.addActionListener(new java.awt.event.ActionListener() {
271
				public void actionPerformed(ActionEvent e) {
272
					useColor_actionPerformed(e);
273
				}
274
			});
275
			pane.add(chkUseColor,c);
276
		}
279
            c.fill = GridBagConstraints.HORIZONTAL;
280
            c.gridwidth = 3;
281
            c.gridx = 1;
282
            c.gridy = 1;
283
            pane.add(sldTransparency, c);
277 284

  
285
            if (withTranspPerc) {
286
                c.fill = GridBagConstraints.HORIZONTAL;
287
                c.gridwidth = 3;
288
                c.gridx = 4;
289
                c.gridy = 1;
290
                pane.add(lblTransparency, c);
291
            }
278 292

  
279
		if (withTransp) {
293
        }
280 294

  
281
			c.fill = GridBagConstraints.HORIZONTAL;
282
			c.gridwidth = 3;
283
			c.gridx = 1;
284
			c.gridy = 1;
285
			pane.add(sldTransparency,c);
295
        add(pane);
296
    }
286 297

  
287

  
288
			if(withTranspPerc) {
289
				c.fill = GridBagConstraints.HORIZONTAL;
290
				c.gridwidth = 3;
291
				c.gridx = 4;
292
				c.gridy = 1;
293
				pane.add(lblTransparency,c);
294
			}
295

  
296
		}
297

  
298
		add(pane);
299
	}
300

  
301 298
    private void changeButton_actionPerformed(ActionEvent e) {
302
    	Color newColor = JColorChooser.showDialog(SwingUtilities.windowForComponent(this), Messages.getText("choose_color"), color);
299
        Color newColor = JColorChooser.showDialog(SwingUtilities.windowForComponent(this), Messages.getText("choose_color"), color);
303 300

  
304 301
        if (newColor == null) {
305 302
            return;
......
310 307
    }
311 308

  
312 309
    /**
313
	 * Returns true if the check box of the color chooser panel is selected.
314
	 * False otherwise.
315
	 * @return boolean true if the check box is selected. Otherwise, false.
316
	 */
317
	public boolean getUseColorisSelected() {
318
		if(withNoFill)
319
			return chkUseColor.isSelected();
320
		return false;
321
	}
322
	/**
323
	 * Sets the check box of the color chooser panel
310
     * Returns true if the check box of the color chooser panel is selected.
311
     * False otherwise.
312
     *
313
     * @return boolean true if the check box is selected. Otherwise, false.
314
     */
315
    public boolean getUseColorisSelected() {
316
        try {
317
            if (withNoFill) {
318
                return chkUseColor.isSelected();
319
            }
320
            return false;
321
        } catch (Exception ex) {
322
            logger.warn("Can't get use-selected-color, return false.",ex);
323
            return false;
324
        }
325
    }
324 326

  
325
	 * @param b boolean true if the user wants to select it.Otherwise, false.
326
	 */
327
	public void setUseColorIsSelected(boolean b) {
328
		if(withNoFill)
329
			chkUseColor.setSelected(b);
330
	}
331
	/**
332
	 * Controls the events when the check box of the color chooser panel
333
	 * is modified
334
	 *
335
	 * @param e Action Event
336
	 */
337
	private void useColor_actionPerformed(ActionEvent e) {
338
		if(chkUseColor.isSelected())
339
			setColor(color);
340
		else setColor(null);
341
		fireActionPerformed();
342
	}
327
    /**
328
     * Sets the check box of the color chooser panel
329
     *
330
     * @param b boolean true if the user wants to select it.Otherwise, false.
331
     */
332
    public void setUseColorIsSelected(boolean b) {
333
        if (withNoFill) {
334
            chkUseColor.setSelected(b);
335
        }
336
    }
343 337

  
344
     public void addActionListener(ActionListener l) {
338
    /**
339
     * Controls the events when the check box of the color chooser panel is
340
     * modified
341
     *
342
     * @param e Action Event
343
     */
344
    private void useColor_actionPerformed(ActionEvent e) {
345
        if (chkUseColor.isSelected()) {
346
            setColor(color);
347
        } else {
348
            setColor(null);
349
        }
350
        fireActionPerformed();
351
    }
352

  
353
    public void addActionListener(ActionListener l) {
345 354
        actionListeners.add(l);
346 355
    }
347 356

  
......
354 363
            i.next().actionPerformed(new ActionEvent(this, 0, null));
355 364
        }
356 365
    }
357
	/**
358
	 * Sets the color of the chooser panel.
359
	 *
360
	 * @param color Color to be selected.
361
	 */
362
	public void setColor(Color color) {
363 366

  
364
		if( color != null) {
365
			this.color = color;
366
			setAlpha(color.getAlpha());
367
			updateColorPanel();
368
		}
367
    /**
368
     * Sets the color of the chooser panel.
369
     *
370
     * @param color Color to be selected.
371
     */
372
    public void setColor(Color color) {
369 373

  
370
	}
374
        if (color != null) {
375
            this.color = color;
376
            setAlpha(color.getAlpha());
377
            updateColorPanel();
378
        }
371 379

  
372
	/**
373
	 * Sets the alpha value of the color selected in the color chooser
374
	 * panel.
375
	 *
376
	 * @param alpha Alpha value of the color.
377
	 */
378
	public void setAlpha(int alpha) {
379
		muteSldTransparency = true;
380
    }
380 381

  
381
		if (color != null)
382
			color=alphaColor(color,alpha);
382
    /**
383
     * Sets the alpha value of the color selected in the color chooser panel.
384
     *
385
     * @param alpha Alpha value of the color.
386
     */
387
    public void setAlpha(int alpha) {
388
        muteSldTransparency = true;
383 389

  
384
		if (withTransp) {
385
			sldTransparency.setValue(alpha);
386
			sldTransparency.validate();
387
			int percValue = (int) (alpha * perc);
388
			lblTransparency.setText( String.valueOf(percValue)+ "%");
389
		}
390
        if (color != null) {
391
            color = alphaColor(color, alpha);
392
        }
390 393

  
391
		updateColorPanel();
392
		muteSldTransparency = false;
394
        if (withTransp) {
395
            sldTransparency.setValue(alpha);
396
            sldTransparency.validate();
397
            int percValue = (int) (alpha * perc);
398
            lblTransparency.setText(String.valueOf(percValue) + "%");
399
        }
400

  
401
        updateColorPanel();
402
        muteSldTransparency = false;
393 403
//		fireActionPerformed();
394
	}
404
    }
395 405

  
396
	public int getAlpha() {
397
		if (withTransp) {
398
			return sldTransparency.getValue();
399
		} else {
400
			return color.getAlpha();
401
		}
406
    public int getAlpha() {
407
        try {
408
            if (withTransp) {
409
                return sldTransparency.getValue();
410
            } else {
411
                return color.getAlpha();
412
            }
413
        } catch (Exception ex) {
414
            logger.warn("Can't get alpha, return 0 as alpha",ex);
415
            return 0;
416
        }
402 417

  
403
	}
418
    }
404 419

  
405 420
    private void updateColorPanel() {
406 421
        colorPanel.setFillColor(color);
407 422
        colorPanel.repaint();
408 423
    }
409 424

  
425
    /**
426
     * Obtains the selected color in the color chooser panel.
427
     *
428
     * @return color the selected color
429
     */
430
    public Color getColor() {
431
        return color;
432
    }
410 433

  
411
	/**
412
	 * Obtains the selected color in the color chooser panel.
413
	 *
414
	 * @return color the selected color
415
	 */
416
	public Color getColor() {
417
		return color;
418
	}
434
    public void disabledWithTransparency() {
435
        changeButton.setEnabled(false);
436
        if (withNoFill) {
437
            chkUseColor.setEnabled(false);
438
        }
419 439

  
420
	public void disabledWithTransparency() {
421
		changeButton.setEnabled(false);
422
		if(withNoFill)
423
			chkUseColor.setEnabled(false);
440
    }
424 441

  
425
	}
442
    @Override
443
    public void setEnabled(boolean newEnabled) {
426 444

  
427
	@Override
428
	public void setEnabled(boolean newEnabled) {
445
        changeButton.setEnabled(newEnabled);
446
        if (withTransp) {
447
            sldTransparency.setEnabled(newEnabled);
448
        }
449
        if (withTranspPerc) {
450
            lblTransparency.setEnabled(newEnabled);
451
        }
452
        if (withNoFill) {
453
            chkUseColor.setEnabled(newEnabled);
454
        }
429 455

  
430
		changeButton.setEnabled(newEnabled);
431
		if(withTransp) {
432
			sldTransparency.setEnabled(newEnabled);
433
		}
434
		if(withTranspPerc) {
435
			lblTransparency.setEnabled(newEnabled);
436
		}
437
		if(withNoFill) {
438
			chkUseColor.setEnabled(newEnabled);
439
		}
456
    }
440 457

  
441
	}
458
    public static void main(String[] args) {
459
        JFrame f = new JFrame();
442 460

  
443
	public static void main(String[] args) {
444
		JFrame f = new JFrame();
461
        final ColorChooserPanel ce2 = new ColorChooserPanel(true, true);
445 462

  
446
		final ColorChooserPanel ce2 = new ColorChooserPanel(true,true);
463
        JPanel content = new JPanel(new GridLayout(2, 1));
447 464

  
448
		JPanel content = new JPanel(new GridLayout(2, 1));
465
        content.add(ce2);
449 466

  
450
		content.add(ce2);
467
        f.setContentPane(content);
468
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
469
        f.pack();
470
        f.setVisible(true);
451 471

  
472
    }
452 473

  
453
		f.setContentPane(content);
454
		f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
455
		f.pack();
456
		f.setVisible(true);
457

  
458
	}
459

  
460 474
    private Color alphaColor(Color color, int alpha) {
461 475
        return new Color(color.getRed(), color.getGreen(), color.getBlue(),
462
            alpha);
476
                alpha);
463 477
    }
464 478

  
465
}
479
}

Also available in: Unified diff