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.compapp.casaeditor.properties;
043:
044: import java.awt.Color;
045: import java.awt.Component;
046: import java.awt.Dialog;
047: import java.awt.Dimension;
048: import java.awt.Font;
049: import java.awt.FontMetrics;
050: import java.awt.Graphics;
051: import java.awt.Rectangle;
052: import java.awt.event.ActionEvent;
053: import java.awt.event.ActionListener;
054: import java.awt.event.WindowAdapter;
055: import java.awt.event.WindowEvent;
056: import java.beans.PropertyEditorSupport;
057: import java.util.LinkedHashMap;
058: import org.netbeans.modules.compapp.casaeditor.Constants;
059: import org.netbeans.modules.compapp.casaeditor.graph.CasaFactory;
060: import org.netbeans.modules.compapp.casaeditor.nodes.LookAndFeelNode;
061: import org.openide.DialogDescriptor;
062: import org.openide.DialogDisplayer;
063: import org.openide.explorer.propertysheet.PropertySheet;
064: import org.openide.nodes.Node;
065: import org.openide.util.NbBundle;
066:
067: /**
068: *
069: * @author jsandusky
070: */
071: public class LookAndFeelEditor extends PropertyEditorSupport {
072:
073: private final static String EMPTY = Constants.EMPTY_STRING;
074:
075: private String mPropertyName;
076: private boolean mIsChangesApplied;
077:
078: private LinkedHashMap<String, Color> originalColors;
079: private LinkedHashMap<String, Font> originalFonts;
080:
081: public enum Option {
082: None, Declared, Other
083: };
084:
085: public LookAndFeelEditor(String propertyName) {
086: mPropertyName = propertyName;
087: }
088:
089: public boolean supportsCustomEditor() {
090: return true;
091: }
092:
093: public String getAsText() {
094: return NbBundle.getMessage(getClass(), "LBL_EditLookAndFeel"); // NOI18N
095: }
096:
097: public void setAsText(String s) {
098: }
099:
100: public boolean isPaintable() {
101: return true;
102: }
103:
104: public void paintValue(Graphics g, Rectangle rectangle) {
105: String paintableString = getPaintableString();
106: FontMetrics metrics = g.getFontMetrics();
107: g.drawString(paintableString, rectangle.x, rectangle.y
108: + (rectangle.height - metrics.getHeight()) / 2
109: + metrics.getAscent());
110: }
111:
112: protected String getPaintableString() {
113: Object value = getAsText();
114: return value == null ? NbBundle.getMessage(StringEditor.class,
115: "LBL_Null") : // NOI18N
116: getAsText();
117: }
118:
119: public Component getCustomEditor() {
120:
121: mIsChangesApplied = false;
122: initialUIState();
123:
124: final PropertySheet propertySheetPanel = new PropertySheet();
125: propertySheetPanel
126: .setNodes(new Node[] { new LookAndFeelNode() });
127:
128: final DialogDescriptor descriptor = new DialogDescriptor(
129: propertySheetPanel, mPropertyName, true,
130: new ActionListener() {
131: public void actionPerformed(ActionEvent evt) {
132: if (evt.getSource().equals(
133: DialogDescriptor.OK_OPTION)) {
134: mIsChangesApplied = true;
135: }
136: }
137: });
138:
139: Dialog dlg = DialogDisplayer.getDefault().createDialog(
140: descriptor);
141: dlg.setPreferredSize(new Dimension(500, 700));
142:
143: dlg.addWindowListener(new WindowAdapter() {
144: public void windowClosed(WindowEvent e) {
145: if (mIsChangesApplied) {
146: applyUIChanges();
147: } else {
148: revertUIChanges();
149: }
150: }
151: });
152:
153: return dlg;
154: }
155:
156: private void initialUIState() {
157: // TODO remember what the original UI properties were
158: originalColors = new LinkedHashMap<String, Color>();
159: originalFonts = new LinkedHashMap<String, Font>();
160:
161: originalColors.putAll(CasaFactory.getCasaCustomizer()
162: .getColorsMapReference());
163: originalFonts.putAll(CasaFactory.getCasaCustomizer()
164: .getFontsMapReference());
165: }
166:
167: private void revertUIChanges() {
168: // TODO revert to the original UI properties
169: CasaFactory.getCasaCustomizer().getColorsMapReference().putAll(
170: originalColors);
171: CasaFactory.getCasaCustomizer().getFontsMapReference().putAll(
172: originalFonts);
173: CasaFactory.getCasaCustomizerRegistor().propagateChange();
174: }
175:
176: private void applyUIChanges() {
177: // TODO persist changes to all UI properties
178: CasaFactory.getCasaCustomizer().savePreferences();
179: }
180: }
|