001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041: package org.netbeans.modules.uihandler.interactive;
042:
043: import org.netbeans.modules.uihandler.*;
044: import java.awt.AlphaComposite;
045: import java.awt.Color;
046: import java.awt.Component;
047: import java.awt.Graphics2D;
048: import java.awt.RenderingHints;
049: import java.awt.event.ActionEvent;
050: import java.awt.event.ActionListener;
051: import java.awt.geom.Arc2D;
052: import java.awt.geom.Area;
053: import java.awt.image.BufferedImage;
054: import java.beans.PropertyChangeEvent;
055: import java.beans.PropertyChangeListener;
056: import javax.swing.Action;
057: import javax.swing.Icon;
058: import javax.swing.ImageIcon;
059: import javax.swing.JButton;
060: import javax.swing.SwingUtilities;
061: import javax.swing.Timer;
062: import org.netbeans.modules.uihandler.api.Controller;
063: import org.openide.awt.Actions;
064: import org.openide.util.HelpCtx;
065: import org.openide.util.NbBundle;
066: import org.openide.util.Utilities;
067: import org.openide.util.WeakListeners;
068: import org.openide.util.actions.CallableSystemAction;
069:
070: public final class SubmitAction extends CallableSystemAction {
071:
072: public void performAction() {
073: Controller.getDefault().submit();
074: }
075:
076: public String getName() {
077: return NbBundle.getMessage(SubmitAction.class,
078: "CTL_SubmitAction");
079: }
080:
081: protected String iconResource() {
082: return "org/netbeans/modules/uihandler/tachometer.png";
083: }
084:
085: public HelpCtx getHelpCtx() {
086: return HelpCtx.DEFAULT_HELP;
087: }
088:
089: protected boolean asynchronous() {
090: return false;
091: }
092:
093: @Override
094: public Component getToolbarPresenter() {
095: return new NrButton(this );
096: }
097:
098: private static final class NrButton extends JButton implements
099: PropertyChangeListener, Runnable, ActionListener {
100: private PropertyChangeListener weakL;
101:
102: private ImageIcon tacho;
103: private ImageIcon tachoOk;
104: private Timer timer;
105:
106: public NrButton(Action action) {
107: Actions.connect(this , action);
108: weakL = WeakListeners.propertyChange(this , Controller
109: .getDefault());
110: Controller.getDefault().addPropertyChangeListener(weakL);
111:
112: timer = new Timer(100, this );
113:
114: tacho = new ImageIcon(
115: Utilities
116: .loadImage("org/netbeans/modules/uihandler/tachometer24.png"));
117: tachoOk = new ImageIcon(
118: Utilities
119: .loadImage("org/netbeans/modules/uihandler/tachometer-ok.png"));
120: setIcon(tacho);
121: setToolTipText(NbBundle.getMessage(SubmitAction.class,
122: "CTL_SubmitAction"));
123: }
124:
125: public void propertyChange(PropertyChangeEvent arg0) {
126: SwingUtilities.invokeLater(this );
127: }
128:
129: public void run() {
130: setIcon(tachoOk);
131: setEnabled(true);
132: timer.restart();
133:
134: setToolTipText(NbBundle.getMessage(SubmitAction.class,
135: "MSG_SubmitAction", Controller.getDefault()
136: .getLogRecordsCount())); // NOI18N
137: }
138:
139: public void actionPerformed(ActionEvent arg0) {
140: setIcon(tacho);
141: timer.stop();
142: }
143:
144: @Override
145: protected void firePropertyChange(String propertyName,
146: Object oldValue, Object newValue) {
147: if ("PreferredIconSize".equals(propertyName)) { // NOI18N
148: run();
149: }
150: super .firePropertyChange(propertyName, oldValue, newValue);
151: }
152:
153: @Override
154: public void setIcon(Icon original) {
155: int size = 16;
156: Object prop = getClientProperty("PreferredIconSize"); //NOI18N
157:
158: if (prop instanceof Integer) {
159: if (((Integer) prop).intValue() == 24) {
160: size = 24;
161: }
162: }
163:
164: BufferedImage img = new BufferedImage(size, size,
165: BufferedImage.TYPE_INT_ARGB);
166: Graphics2D imgG = (Graphics2D) img.getGraphics();
167: imgG.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
168: RenderingHints.VALUE_ANTIALIAS_ON);
169:
170: imgG.drawImage(((ImageIcon) original).getImage(), 0, 0,
171: size, size, null);
172:
173: int half = size / 2;
174: final Arc2D bigger = new Arc2D.Double();
175: bigger.setArcByCenter(half, half, half, 90,
176: -(360.0 / 1000.0)
177: * Controller.getDefault()
178: .getLogRecordsCount(), Arc2D.PIE);
179: final Arc2D smaller = new Arc2D.Double();
180: smaller.setArcByCenter(half, half, size == 24 ? 5.0 : 3.0,
181: 0, 360, Arc2D.PIE);
182:
183: int s = Controller.getDefault().getLogRecordsCount();
184: if (s < 800) {
185: imgG.setColor(Color.RED.darker().darker());
186: } else if (s < 990) {
187: imgG.setColor(Color.ORANGE);
188: } else {
189: imgG.setColor(Color.RED);
190: }
191: imgG.setComposite(AlphaComposite.getInstance(
192: AlphaComposite.SRC_OVER, 0.5f));
193:
194: Area minus = new Area(bigger);
195: minus.subtract(new Area(smaller));
196: imgG.fill(minus);
197:
198: super .setIcon(new ImageIcon(img));
199: }
200: } // end of NrButton
201: }
|