KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > paint > PaintTopComponent


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.paint;
21
22 import java.awt.BorderLayout JavaDoc;
23 import java.awt.Dimension JavaDoc;
24 import java.awt.event.ActionEvent JavaDoc;
25 import java.awt.event.ActionListener JavaDoc;
26 import java.awt.image.BufferedImage JavaDoc;
27 import java.io.File JavaDoc;
28 import java.io.IOException JavaDoc;
29 import javax.imageio.ImageIO JavaDoc;
30 import javax.swing.JButton JavaDoc;
31 import javax.swing.JComponent JavaDoc;
32 import javax.swing.JFileChooser JavaDoc;
33 import javax.swing.JLabel JavaDoc;
34 import javax.swing.JOptionPane JavaDoc;
35 import javax.swing.JSlider JavaDoc;
36 import javax.swing.JToolBar JavaDoc;
37 import javax.swing.event.ChangeEvent JavaDoc;
38 import javax.swing.event.ChangeListener JavaDoc;
39 import org.netbeans.swing.colorchooser.ColorChooser;
40 import org.openide.awt.StatusDisplayer;
41 import org.openide.util.NbBundle;
42 import org.openide.windows.TopComponent;
43
44 public class PaintTopComponent extends TopComponent implements ActionListener JavaDoc, ChangeListener JavaDoc {
45     
46     private static int tcCount = 0; //A counter to limit number of simultaneously existing images
47
private static int ct = 0; //A counter we use to provide names for new images
48

49     static int getPaintTCCount() {
50         return tcCount;
51     }
52     
53     private final PaintCanvas canvas = new PaintCanvas(); //The component the user draws on
54
private JComponent JavaDoc preview; //A component in the toolbar that shows the paintbrush size
55

56     /** Creates a new instance of PaintTopComponent */
57     public PaintTopComponent() {
58         initComponents();
59         String JavaDoc displayName = NbBundle.getMessage(
60                 PaintTopComponent.class,
61                 "UnsavedImageNameFormat",
62                 new Object JavaDoc[] { new Integer JavaDoc(ct++) }
63         );
64         tcCount++;
65         setName(displayName);
66         setDisplayName(displayName);
67     }
68     
69     public void actionPerformed(ActionEvent JavaDoc e) {
70         if (e.getSource() instanceof JButton JavaDoc) {
71             canvas.clear();
72         } else if (e.getSource() instanceof ColorChooser) {
73             ColorChooser cc = (ColorChooser) e.getSource();
74             canvas.setPaint(cc.getColor());
75         }
76         preview.paintImmediately(0, 0, preview.getWidth(), preview.getHeight());
77     }
78     
79     public void stateChanged(ChangeEvent JavaDoc e) {
80         JSlider JavaDoc js = (JSlider JavaDoc) e.getSource();
81         canvas.setDiam(js.getValue());
82         preview.paintImmediately(0, 0, preview.getWidth(), preview.getHeight());
83     }
84     
85     public int getPersistenceType() {
86         return PERSISTENCE_NEVER;
87     }
88     
89     private void initComponents() {
90         setLayout(new BorderLayout JavaDoc());
91         JToolBar JavaDoc bar = new JToolBar JavaDoc();
92         
93         ColorChooser fg = new ColorChooser();
94         preview = canvas.createBrushSizeView();
95         
96         //Now build our toolbar:
97

98         //Make sure components don't get squished:
99
Dimension JavaDoc min = new Dimension JavaDoc(32, 32);
100         preview.setMaximumSize(min);
101         fg.setPreferredSize(new Dimension JavaDoc(16, 16));
102         fg.setMinimumSize(min);
103         fg.setMaximumSize(min);
104         
105         JButton JavaDoc clear = new JButton JavaDoc(
106                 NbBundle.getMessage(PaintTopComponent.class, "LBL_Clear"));
107         
108         JLabel JavaDoc fore = new JLabel JavaDoc(
109                 NbBundle.getMessage(PaintTopComponent.class, "LBL_Foreground"));
110         
111         fg.addActionListener(this);
112         clear.addActionListener(this);
113         
114         JSlider JavaDoc js = new JSlider JavaDoc();
115         js.setMinimum(1);
116         js.setMaximum(24);
117         js.setValue(canvas.getDiam());
118         js.addChangeListener(this);
119         
120         fg.setColor(canvas.getColor());
121         
122         bar.add(clear);
123         bar.add(fore);
124         bar.add(fg);
125         JLabel JavaDoc bsize = new JLabel JavaDoc(
126                 NbBundle.getMessage(PaintTopComponent.class, "LBL_BrushSize"));
127         
128         bar.add(bsize);
129         bar.add(js);
130         bar.add(preview);
131         
132         JLabel JavaDoc spacer = new JLabel JavaDoc(" "); //Just a spacer so the brush preview
133
//isn't stretched to the end of the
134
//toolbar
135

136         spacer.setPreferredSize(new Dimension JavaDoc(400, 24));
137         bar.add(spacer);
138         
139         //And install the toolbar and the painting component:
140
add(bar, BorderLayout.NORTH);
141         add(canvas, BorderLayout.CENTER);
142     }
143     
144     public void saveAs() throws IOException JavaDoc {
145         JFileChooser JavaDoc ch = new JFileChooser JavaDoc();
146         if (ch.showSaveDialog(this) == JFileChooser.APPROVE_OPTION &&
147                 ch.getSelectedFile() != null) {
148             
149             File JavaDoc f = ch.getSelectedFile();
150             if (!f.getPath().endsWith(".png")) {
151                 f = new File JavaDoc(f.getPath() + ".png");
152             }
153             if (!f.exists()) {
154                 if (!f.createNewFile()) {
155                     String JavaDoc failMsg = NbBundle.getMessage(
156                             PaintTopComponent.class,
157                             "MSG_SaveFailed", new Object JavaDoc[] { f.getPath() }
158                     );
159                     JOptionPane.showMessageDialog(this, failMsg);
160                     return;
161                 }
162             } else {
163                 String JavaDoc overwriteMsg = NbBundle.getMessage(
164                         PaintTopComponent.class,
165                         "MSG_Overwrite", new Object JavaDoc[] { f.getPath() }
166                 );
167                 if (JOptionPane.showConfirmDialog(this, overwriteMsg)
168                 != JOptionPane.OK_OPTION) {
169                     
170                     return;
171                 }
172             }
173             doSave(f);
174         }
175     }
176     
177     private void doSave(File JavaDoc f) throws IOException JavaDoc {
178         BufferedImage JavaDoc img = canvas.getImage();
179         ImageIO.write(img, "png", f);
180         String JavaDoc statusMsg = NbBundle.getMessage(PaintTopComponent.class,
181                 "MSG_Saved", new Object JavaDoc[] { f.getPath() });
182         StatusDisplayer.getDefault().setStatusText(statusMsg);
183         setDisplayName(f.getName());
184     }
185
186     protected void componentClosed() {
187         super.componentClosed();
188         tcCount--;
189     }
190     
191 }
192
Popular Tags