001: package net.xoetrope.builder.editor.dialog;
002:
003: import java.awt.BorderLayout;
004: import java.awt.Color;
005: import java.awt.Component;
006: import java.awt.Dimension;
007: import java.awt.Font;
008: import java.awt.GridLayout;
009: import java.awt.event.ActionEvent;
010: import java.awt.event.ActionListener;
011: import javax.swing.JButton;
012: import javax.swing.JColorChooser;
013: import javax.swing.JDialog;
014: import javax.swing.JLabel;
015: import javax.swing.JPanel;
016: import javax.swing.JTextField;
017: import javax.swing.border.EmptyBorder;
018:
019: import net.xoetrope.builder.editor.XEditorProject;
020: import net.xoetrope.builder.editor.XEditorStyleManager;
021: import net.xoetrope.builder.editor.XEditorUtilities;
022: import net.xoetrope.builder.editor.XPaletteComponentFactory;
023: import net.xoetrope.builder.editor.events.StyleListener;
024: import net.xoetrope.xui.style.XStyle;
025: import net.xoetrope.xui.XProjectManager;
026: import javax.swing.JOptionPane;
027:
028: /**
029: * <p> Copyright (c) Xoetrope Ltd., 2002-2003</p>
030: * <p> $Revision: 1.6 $</p>
031: * <p> License: see License.txt</p>
032: */
033: public class XStyleEditorDialog extends JDialog implements
034: ActionListener {
035: private JLabel styleForegroundClr, styleBackgroundClr, styleFont;
036: private JButton btnOK, btnCancel;
037: private JTextField txtStyleName;
038: private JPanel pnlControls;
039:
040: private XEditorProject currentProject;
041:
042: private StyleListener styleListener;
043: private XEditorStyleManager styleManager;
044: private boolean status = false;
045: private XStyle currentStyle;
046: private String addStylePath;
047:
048: private boolean addMode;
049:
050: public XStyleEditorDialog(boolean addMode, XStyle aStyle,
051: String styleName) {
052: setModal(true);
053: this .addMode = addMode;
054: setTitle(addMode ? "Add style" : "Edit style");
055: setSize(230, 180);
056: if (addMode)
057: currentStyle = (XStyle) aStyle.clone();
058: else
059: currentStyle = aStyle;
060:
061: getContentPane().setLayout(new BorderLayout());
062: pnlControls = new JPanel(new GridLayout(0, 1));
063: pnlControls.setPreferredSize(new Dimension(230, 1));
064: pnlControls.setBorder(new EmptyBorder(8, 8, 8, 8));
065:
066: txtStyleName = XPaletteComponentFactory.addProperty(
067: "Style name :", null, false, pnlControls, this , 69);
068: txtStyleName.setEnabled(addMode);
069: styleBackgroundClr = XPaletteComponentFactory
070: .addLabelProperty("Background: ", pnlControls,
071: "showColorDlg", this , true);
072: styleForegroundClr = XPaletteComponentFactory
073: .addLabelProperty("Foreground: ", pnlControls,
074: "showColorDlg", this , true);
075: styleFont = XPaletteComponentFactory.addLabelProperty("Font: ",
076: pnlControls, "showFontDlg", this , true);
077: styleFont.setText("Sample text");
078: styleBackgroundClr.setBackground(currentStyle
079: .getStyleAsColor(XStyle.COLOR_BACK));
080: styleForegroundClr.setBackground(currentStyle
081: .getStyleAsColor(XStyle.COLOR_FORE));
082: if (addMode)
083: addStylePath = styleName;
084: else
085: txtStyleName.setText(styleName);
086:
087: styleFont.setForeground(currentStyle
088: .getStyleAsColor(XStyle.COLOR_FORE));
089: styleFont.setBackground(currentStyle
090: .getStyleAsColor(XStyle.COLOR_BACK));
091: int fontitalic = currentStyle.getStyleAsInt(XStyle.FONT_ITALIC);
092: int fontweight = currentStyle.getStyleAsInt(XStyle.FONT_WEIGHT);
093: int fontStyle = 0;
094: if (fontweight == 1)
095: fontStyle = Font.BOLD;
096: if (fontitalic == 1)
097: fontStyle = fontStyle | Font.ITALIC;
098: styleFont.setFont(new Font(currentStyle
099: .getStyleAsString(XStyle.FONT_FACE), fontStyle,
100: currentStyle.getStyleAsInt(XStyle.FONT_SIZE)));
101:
102: /* Add the buttons... */
103: JPanel panel = new JPanel(new GridLayout(1, 3));
104: panel.setBorder(new EmptyBorder(8, 8, 8, 8));
105: getContentPane().add(panel, BorderLayout.SOUTH);
106: panel.setName("XPageBuilder_OptionalProperty");
107:
108: btnCancel = setupButton(panel, "Cancel", "newStyle", true);
109: btnOK = setupButton(panel, "OK", "saveStyle", true);
110:
111: getContentPane().add(pnlControls, BorderLayout.CENTER);
112: pnlControls.doLayout();
113: doLayout();
114: }
115:
116: public boolean getStatus() {
117: return status;
118: }
119:
120: public XStyle getStyle() {
121: return currentStyle;
122: }
123:
124: public String getStyleName() {
125: return txtStyleName.getText();
126: }
127:
128: public void actionPerformed(ActionEvent e) {
129: Object source = e.getSource();
130: if (source instanceof JButton) {
131: JButton popupBtn = (JButton) source;
132: String actionCmd = popupBtn.getActionCommand();
133: if (source == btnOK) {
134: if (addMode) {
135: styleManager = (XEditorStyleManager) XProjectManager
136: .getStyleManager();
137: String tempName = addStylePath;
138: if (tempName.length() > 0)
139: tempName += "/";
140: tempName += txtStyleName.getText();
141: XStyle newStyle = styleManager.getStyle(tempName,
142: false);
143: if (newStyle != null)
144: JOptionPane.showMessageDialog(this ,
145: "This style already exists!");
146: else {
147: status = true;
148: setVisible(false);
149: }
150: } else {
151: status = true;
152: setVisible(false);
153: }
154: } else if (source == btnCancel) {
155: status = false;
156: setVisible(false);
157: } else if (actionCmd.compareTo("showColorDlg") == 0) {
158: JColorChooser colorChooser = new JColorChooser();
159: XEditorUtilities.setDefaultFont(colorChooser);
160: Component targetProperty = popupBtn.getParent()
161: .getComponent(1);
162: Color newColor = colorChooser.showDialog(popupBtn,
163: "Select required color", targetProperty
164: .getBackground());
165: if (newColor != null) {
166: targetProperty.setBackground(newColor);
167: if (targetProperty.equals(styleForegroundClr)) {
168: styleFont.setForeground(newColor);
169: currentStyle.setStyle(XStyle.COLOR_FORE,
170: newColor);
171: } else if (targetProperty
172: .equals(styleBackgroundClr)) {
173: styleFont.setBackground(newColor);
174: currentStyle.setStyle(XStyle.COLOR_BACK,
175: newColor);
176: }
177: }
178: } else if (actionCmd.compareTo("showFontDlg") == 0) {
179: XFontChooserDialog fcd = new XFontChooserDialog(null,
180: styleFont.getFont(),
181: "Select the required font!", "Sample text");
182: fcd.setLocationRelativeTo(this );
183: Font newFont = fcd.showDialog();
184: if (newFont != null) {
185: styleFont.setFont(newFont);
186: currentStyle.setStyle(XStyle.FONT_FACE, newFont
187: .getFamily());
188: currentStyle.setStyle(XStyle.FONT_SIZE, newFont
189: .getSize());
190: currentStyle.setStyle(XStyle.FONT_WEIGHT, newFont
191: .isBold() ? 1 : 0);
192: currentStyle.setStyle(XStyle.FONT_ITALIC, newFont
193: .isItalic() ? 1 : 0);
194: }
195: }
196: }
197: }
198:
199: /**
200: * Adds a button for the dialog
201: * @param panel the panel to which the button will be added
202: * @param caption the button caption
203: * @param command the command name
204: * @param isEnabled true to enable the button
205: * @return the new button
206: */
207: private JButton setupButton(JPanel panel, String caption,
208: String command, boolean isEnabled) {
209: JButton btn = new JButton(caption);
210: btn.addActionListener(this );
211: btn.setPreferredSize(new Dimension(100, 25));
212: panel.add(btn);
213:
214: return btn;
215: }
216: }
|