001: /*
002: * Copyright (c) 2007, Sun Microsystems, Inc. All rights reserved.
003: *
004: * Redistribution and use in source and binary forms, with or without
005: * modification, are permitted provided that the following conditions are met:
006: *
007: * * Redistributions of source code must retain the above copyright notice,
008: * this list of conditions and the following disclaimer.
009: *
010: * * Redistributions in binary form must reproduce the above copyright notice,
011: * this list of conditions and the following disclaimer in the documentation
012: * and/or other materials provided with the distribution.
013: *
014: * * Neither the name of Sun Microsystems, Inc. nor the names of its contributors
015: * may be used to endorse or promote products derived from this software without
016: * specific prior written permission.
017: *
018: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
019: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
020: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
021: * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
022: * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
023: * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
024: * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
025: * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
026: * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
027: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
028: * THE POSSIBILITY OF SUCH DAMAGE.
029: */
030:
031: package org.netbeans.paint;
032:
033: import java.awt.BorderLayout;
034: import java.awt.Dimension;
035: import java.awt.event.ActionEvent;
036: import java.awt.event.ActionListener;
037: import java.awt.image.BufferedImage;
038: import java.io.File;
039: import java.io.IOException;
040: import javax.imageio.ImageIO;
041: import javax.swing.JButton;
042: import javax.swing.JComponent;
043: import javax.swing.JFileChooser;
044: import javax.swing.JLabel;
045: import javax.swing.JOptionPane;
046: import javax.swing.JSlider;
047: import javax.swing.JToolBar;
048: import javax.swing.event.ChangeEvent;
049: import javax.swing.event.ChangeListener;
050: import org.netbeans.swing.colorchooser.ColorChooser;
051: import org.openide.awt.StatusDisplayer;
052: import org.openide.util.NbBundle;
053: import org.openide.windows.TopComponent;
054:
055: public class PaintTopComponent extends TopComponent implements
056: ActionListener, ChangeListener {
057:
058: private static int tcCount = 0; //A counter to limit number of simultaneously existing images
059: private static int ct = 0; //A counter we use to provide names for new images
060:
061: static int getPaintTCCount() {
062: return tcCount;
063: }
064:
065: private final PaintCanvas canvas = new PaintCanvas(); //The component the user draws on
066: private JComponent preview; //A component in the toolbar that shows the paintbrush size
067:
068: /** Creates a new instance of PaintTopComponent */
069: public PaintTopComponent() {
070: initComponents();
071: String displayName = NbBundle.getMessage(
072: PaintTopComponent.class, "UnsavedImageNameFormat",
073: new Object[] { new Integer(ct++) });
074: tcCount++;
075: setName(displayName);
076: setDisplayName(displayName);
077: }
078:
079: public void actionPerformed(ActionEvent e) {
080: if (e.getSource() instanceof JButton) {
081: canvas.clear();
082: } else if (e.getSource() instanceof ColorChooser) {
083: ColorChooser cc = (ColorChooser) e.getSource();
084: canvas.setPaint(cc.getColor());
085: }
086: preview.paintImmediately(0, 0, preview.getWidth(), preview
087: .getHeight());
088: }
089:
090: public void stateChanged(ChangeEvent e) {
091: JSlider js = (JSlider) e.getSource();
092: canvas.setDiam(js.getValue());
093: preview.paintImmediately(0, 0, preview.getWidth(), preview
094: .getHeight());
095: }
096:
097: public int getPersistenceType() {
098: return PERSISTENCE_NEVER;
099: }
100:
101: private void initComponents() {
102: setLayout(new BorderLayout());
103: JToolBar bar = new JToolBar();
104:
105: ColorChooser fg = new ColorChooser();
106: preview = canvas.createBrushSizeView();
107:
108: //Now build our toolbar:
109:
110: //Make sure components don't get squished:
111: Dimension min = new Dimension(32, 32);
112: preview.setMaximumSize(min);
113: fg.setPreferredSize(new Dimension(16, 16));
114: fg.setMinimumSize(min);
115: fg.setMaximumSize(min);
116:
117: JButton clear = new JButton(NbBundle.getMessage(
118: PaintTopComponent.class, "LBL_Clear"));
119:
120: JLabel fore = new JLabel(NbBundle.getMessage(
121: PaintTopComponent.class, "LBL_Foreground"));
122:
123: fg.addActionListener(this );
124: clear.addActionListener(this );
125:
126: JSlider js = new JSlider();
127: js.setMinimum(1);
128: js.setMaximum(24);
129: js.setValue(canvas.getDiam());
130: js.addChangeListener(this );
131:
132: fg.setColor(canvas.getColor());
133:
134: bar.add(clear);
135: bar.add(fore);
136: bar.add(fg);
137: JLabel bsize = new JLabel(NbBundle.getMessage(
138: PaintTopComponent.class, "LBL_BrushSize"));
139:
140: bar.add(bsize);
141: bar.add(js);
142: bar.add(preview);
143:
144: JLabel spacer = new JLabel(" "); //Just a spacer so the brush preview
145: //isn't stretched to the end of the
146: //toolbar
147:
148: spacer.setPreferredSize(new Dimension(400, 24));
149: bar.add(spacer);
150:
151: //And install the toolbar and the painting component:
152: add(bar, BorderLayout.NORTH);
153: add(canvas, BorderLayout.CENTER);
154: }
155:
156: public void saveAs() throws IOException {
157: JFileChooser ch = new JFileChooser();
158: if (ch.showSaveDialog(this ) == JFileChooser.APPROVE_OPTION
159: && ch.getSelectedFile() != null) {
160:
161: File f = ch.getSelectedFile();
162: if (!f.getPath().endsWith(".png")) {
163: f = new File(f.getPath() + ".png");
164: }
165: if (!f.exists()) {
166: if (!f.createNewFile()) {
167: String failMsg = NbBundle.getMessage(
168: PaintTopComponent.class, "MSG_SaveFailed",
169: new Object[] { f.getPath() });
170: JOptionPane.showMessageDialog(this , failMsg);
171: return;
172: }
173: } else {
174: String overwriteMsg = NbBundle.getMessage(
175: PaintTopComponent.class, "MSG_Overwrite",
176: new Object[] { f.getPath() });
177: if (JOptionPane.showConfirmDialog(this , overwriteMsg) != JOptionPane.OK_OPTION) {
178:
179: return;
180: }
181: }
182: doSave(f);
183: }
184: }
185:
186: private void doSave(File f) throws IOException {
187: BufferedImage img = canvas.getImage();
188: ImageIO.write(img, "png", f);
189: String statusMsg = NbBundle.getMessage(PaintTopComponent.class,
190: "MSG_Saved", new Object[] { f.getPath() });
191: StatusDisplayer.getDefault().setStatusText(statusMsg);
192: setDisplayName(f.getName());
193: }
194:
195: protected void componentClosed() {
196: super.componentClosed();
197: tcCount--;
198: }
199:
200: }
|