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:
042: package org.netbeans.modules.visualweb.designer.jsf.action;
043:
044: import java.util.prefs.PreferenceChangeEvent;
045: import java.util.prefs.PreferenceChangeListener;
046: import javax.swing.AbstractAction;
047: import java.awt.Component;
048: import java.awt.Dimension;
049: import java.awt.event.ActionEvent;
050: import java.awt.event.ActionListener;
051: import java.beans.PropertyChangeEvent;
052: import java.beans.PropertyChangeListener;
053: import javax.swing.Action;
054: import javax.swing.DefaultListCellRenderer;
055: import javax.swing.JComboBox;
056: import javax.swing.JList;
057: import javax.swing.JMenuItem;
058: import javax.swing.event.ChangeListener;
059: import org.netbeans.modules.visualweb.designer.jsf.JsfDesignerPreferences;
060: import org.openide.awt.Actions;
061: import org.openide.util.HelpCtx;
062: import org.openide.util.NbBundle;
063: import org.openide.util.WeakListeners;
064: import org.openide.util.actions.Presenter;
065:
066: /**
067: * Action providing target resolutions.
068: *
069: *
070: * @author Peter Zavadsky
071: * @author Tor Norbye (old functionality implementation -> performActionAt impl)
072: */
073: public class TargetResolutionAction extends AbstractAction implements
074: Presenter.Menu, Presenter.Popup, Presenter.Toolbar {
075:
076: /** Creates a new instance of TargetResolutionAction. */
077: public TargetResolutionAction() {
078: putValue(NAME, getDisplayName());
079: }
080:
081: private String getDisplayName() {
082: return NbBundle.getMessage(TargetResolutionAction.class,
083: "LBL_TargetResolutionAction");
084: }
085:
086: // private String getIconBase(Element[] componentRootElements) {
087: // return null;
088: // }
089:
090: // private boolean isEnabled() {
091: // return componentRootElements.length > 0;
092: // }
093:
094: // private void performAction() {
095: // // XXX Strange impl of the Actions.SubMenu(action, model, isPopup). If the model provides one item,
096: // // it doesn't call the performAt(0), but this method.
097: //// new ResolutionMenuModel(designBeans).performActionAt(0);
098: // RESOLUTIONS[0].performAction();
099: // }
100:
101: public void actionPerformed(ActionEvent e) {
102: RESOLUTIONS[0].performAction();
103: }
104:
105: public JMenuItem getMenuPresenter() {
106: return new Actions.SubMenu(this , new ResolutionMenuModel(),
107: false);
108: }
109:
110: public JMenuItem getPopupPresenter() {
111: return new Actions.SubMenu(this , new ResolutionMenuModel(),
112: true);
113: }
114:
115: public Component getToolbarPresenter() {
116: return new TargetResolutionComboBox(this );
117: }
118:
119: private static class TargetResolutionComboBox extends JComboBox
120: implements PropertyChangeListener, PreferenceChangeListener {
121: private final Action contextAwareAction;
122:
123: public TargetResolutionComboBox(Action contextAwareAction) {
124: super (RESOLUTIONS);
125:
126: this .contextAwareAction = contextAwareAction;
127:
128: // XXX Do better than annonymous class.
129: setRenderer(new DefaultListCellRenderer() {
130: public Component getListCellRendererComponent(
131: JList list, Object value, int index,
132: boolean isSelected, boolean cellHasFocus) {
133: if (value instanceof Resolution) {
134: value = ((Resolution) value).getDisplayName();
135: }
136: return super .getListCellRendererComponent(list,
137: value, index, isSelected, cellHasFocus);
138: }
139: });
140:
141: contextAwareAction.addPropertyChangeListener(WeakListeners
142: .propertyChange(this , contextAwareAction));
143: // #6457867.
144: // DesignerSettings.getInstance().addPropertyChangeListener(WeakListeners.propertyChange(this, DesignerSettings.getInstance()));
145: // DesignerSettings.getInstance().addWeakPreferenceChangeListener(this);
146: JsfDesignerPreferences.getInstance()
147: .addWeakPreferenceChangeListener(this );
148:
149: setSelectedItemForComboBox(this );
150:
151: // XXX Do better than annonymous class.
152: addActionListener(new ActionListener() {
153: public void actionPerformed(ActionEvent evt) {
154: Resolution resolution = (Resolution) getSelectedItem();
155: if (resolution != null) {
156: resolution.performAction();
157: }
158: }
159: });
160:
161: // XXX Copied from former TargetSizeCombo.
162: // Set fixed width to prevent excessive horizontal expansion
163: // etc. Similar to hack I saw in NetBeans NavigationView code
164: // where they're putting dropdowns in the toolbar too
165: Dimension prefSize = getPreferredSize();
166: setPreferredSize(prefSize);
167: setMinimumSize(prefSize);
168: setMaximumSize(prefSize);
169: }
170:
171: public void propertyChange(PropertyChangeEvent evt) {
172: // if (PROP_ELEMENTS.equals(evt.getPropertyName())) {
173: //// DesignBean[] designBeans = (DesignBean[])evt.getNewValue();
174: //// this.designBeans = designBeans;
175: // setSelectedItemForComboBox(this);
176: // } else
177: // if (DesignerSettings.PROP_PAGE_SIZE.equals(evt.getPropertyName())) {
178: // // #6457867 If the change originated from other source, update the combo selection.
179: // setSelectedItemForComboBox(this);
180: // }
181: }
182:
183: private static void setSelectedItemForComboBox(JComboBox combo) {
184: int type = JsfDesignerPreferences.getInstance()
185: .getPageSize();
186: Object selected = null;
187: for (int i = 0; i < RESOLUTIONS.length; i++) {
188: if (type == RESOLUTIONS[i].getType()) {
189: selected = RESOLUTIONS[i];
190: break;
191: }
192: }
193: combo.setSelectedItem(selected);
194: }
195:
196: public void preferenceChange(PreferenceChangeEvent evt) {
197: if (JsfDesignerPreferences.PROP_PAGE_SIZE.equals(evt
198: .getKey())) {
199: // #6457867 If the change originated from other source, update the combo selection.
200: setSelectedItemForComboBox(this );
201: }
202: }
203: }
204:
205: // XXX Make an enum once moved to jdk5.0 sources.
206: private static class Resolution {
207: private final int type;
208: private final String displayName;
209:
210: public Resolution(int type, String displayName) {
211: this .type = type;
212: this .displayName = displayName;
213: }
214:
215: public int getType() {
216: return type;
217: }
218:
219: public String getDisplayName() {
220: return displayName;
221: }
222:
223: public void performAction() {
224: setResolution(type);
225: }
226: } // End of Resolution class.
227:
228: private static final Resolution RESOLUTION_NONE = new Resolution(
229: JsfDesignerPreferences.CONSTRAINTS_NONE, NbBundle
230: .getMessage(TargetResolutionAction.class,
231: "LBL_ResolutionNone"));
232: private static final Resolution RESOLUTION_640x480 = new Resolution(
233: JsfDesignerPreferences.CONSTRAINTS_640x480, NbBundle
234: .getMessage(TargetResolutionAction.class,
235: "LBL_Resolution640x480"));
236: private static final Resolution RESOLUTION_800x600 = new Resolution(
237: JsfDesignerPreferences.CONSTRAINTS_800x600, NbBundle
238: .getMessage(TargetResolutionAction.class,
239: "LBL_Resolution800x600"));
240: private static final Resolution RESOLUTION_1024x768 = new Resolution(
241: JsfDesignerPreferences.CONSTRAINTS_1024x768, NbBundle
242: .getMessage(TargetResolutionAction.class,
243: "LBL_Resolution1024x768"));
244: private static final Resolution RESOLUTION_1280x1024 = new Resolution(
245: JsfDesignerPreferences.CONSTRAINTS_1280x1024, NbBundle
246: .getMessage(TargetResolutionAction.class,
247: "LBL_Resolution1280x1024"));
248:
249: private static final Resolution[] RESOLUTIONS = new Resolution[] {
250: RESOLUTION_NONE, RESOLUTION_640x480, RESOLUTION_800x600,
251: RESOLUTION_1024x768, RESOLUTION_1280x1024 };
252:
253: /** Implementation of the actions submenu model. */
254: private static class ResolutionMenuModel implements
255: Actions.SubMenuModel {
256:
257: public ResolutionMenuModel() {
258: }
259:
260: public int getCount() {
261: return RESOLUTIONS.length;
262: }
263:
264: public String getLabel(int i) {
265: return RESOLUTIONS[i].getDisplayName();
266: }
267:
268: public HelpCtx getHelpCtx(int i) {
269: // XXX Implement?
270: return null;
271: }
272:
273: public void performActionAt(int i) {
274: RESOLUTIONS[i].performAction();
275: }
276:
277: public void addChangeListener(ChangeListener changeListener) {
278: // dummy, this model is not mutable.
279: }
280:
281: public void removeChangeListener(ChangeListener changeListener) {
282: // dummy, this model is not mutable.
283: }
284:
285: } // End of ResolutionMenuModel.
286:
287: // XXX Copied from before DesignerActions
288: /** Changes the target resolution. */
289: private static void setResolution(int type) {
290: if (type != JsfDesignerPreferences.getInstance().getPageSize()) {
291: JsfDesignerPreferences.getInstance().setPageSize(type);
292: }
293: }
294:
295: }
|