001: /*
002: * Created on Jun 29, 2004
003: *
004: * This file is part of Thingamablog. ( http://thingamablog.sf.net )
005: *
006: * Copyright (c) 2004, Bob Tantlinger All Rights Reserved.
007: *
008: * This program is free software; you can redistribute it and/or
009: * modify it under the terms of the GNU General Public License
010: * as published by the Free Software Foundation; either version 2
011: * of the License, or (at your option) any later version.
012: *
013: * This program is distributed in the hope that it will be useful,
014: * but WITHOUT ANY WARRANTY; without even the implied warranty of
015: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
016: * GNU General Public License for more details.
017: *
018: * You should have received a copy of the GNU General Public License
019: * along with this program; if not, write to the Free Software
020: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
021: * USA.
022: *
023: */
024: package net.sf.thingamablog.gui.properties;
025:
026: import java.awt.BorderLayout;
027: import java.awt.Component;
028: import java.awt.Dialog;
029: import java.awt.Frame;
030:
031: import javax.swing.BorderFactory;
032: import javax.swing.JLabel;
033: import javax.swing.JOptionPane;
034: import javax.swing.JPanel;
035: import javax.swing.JScrollPane;
036: import javax.swing.JTextArea;
037: import javax.swing.JTextField;
038:
039: import net.atlanticbb.tantlinger.i18n.I18n;
040: import net.atlanticbb.tantlinger.ui.text.TextEditPopupManager;
041: import net.sf.thingamablog.blog.TBWeblog;
042: import net.sf.thingamablog.generator.CustomTag;
043: import net.sf.thingamablog.gui.StandardDialog;
044:
045: /**
046: * @author Bob Tantlinger
047: *
048: *
049: *
050: */
051: public class TBCustomVariablesPanel extends PropertyPanel {
052: /**
053: *
054: */
055: private static final long serialVersionUID = 1L;
056:
057: private static final I18n i18n = I18n
058: .getInstance("net.sf.thingamablog.gui.properties");
059:
060: private TBWeblog weblog;
061: private EditableList eList;
062:
063: public TBCustomVariablesPanel(TBWeblog wb) {
064: weblog = wb;
065:
066: eList = new EditableList(new VarEditableListModel());
067: eList
068: .setBorder(BorderFactory.createEmptyBorder(12, 12, 12,
069: 12));
070: eList.setListData(weblog.getPageGenerator().getCustomTags());
071:
072: setLayout(new BorderLayout(5, 5));
073: add(eList, BorderLayout.CENTER);
074: }
075:
076: /* (non-Javadoc)
077: * @see net.sf.thingamablog.gui.properties.PropertyPanel#savePropertiesToWeblog()
078: */
079: public void saveProperties() {
080: weblog.setPublishAll(true);
081: java.util.Vector v = eList.getListData();
082: CustomTag tags[] = new CustomTag[v.size()];
083: for (int i = 0; i < tags.length; i++)
084: tags[i] = (CustomTag) v.elementAt(i);
085:
086: weblog.getPageGenerator().setCustomTags(tags);
087: }
088:
089: /* (non-Javadoc)
090: * @see net.sf.thingamablog.gui.properties.PropertyPanel#isValidData()
091: */
092: public boolean isValidData() {
093: return true;
094: }
095:
096: private class VarEditableListModel implements EditableListModel {
097: public Object add(EditableList c) {
098: VariableEditor ed = createEditor();
099: ed.setLocationRelativeTo(c);
100: ed.setVisible(true);
101: if (ed.hasUserCancelled())
102: return null;
103:
104: return ed.getVariable();
105: }
106:
107: public boolean shouldRemove(Object o, EditableList c) {
108: return true;
109: }
110:
111: public Object edit(Object o, EditableList c) {
112: VariableEditor ed = createEditor();
113: CustomTag v = (CustomTag) o;
114: ed.setVariable(v);
115: ed.setLocationRelativeTo(c);
116: ed.setVisible(true);
117: if (ed.hasUserCancelled())
118: return null;
119:
120: return ed.getVariable();
121: }
122:
123: private VariableEditor createEditor() {
124: Component c = getParent();
125: while (c.getParent() != null)
126: c = c.getParent();
127:
128: VariableEditor d;
129: if (c instanceof Frame)
130: d = new VariableEditor((Frame) c);
131: else if (c instanceof Dialog)
132: d = new VariableEditor((Dialog) c);
133: else
134: d = new VariableEditor();
135:
136: return d;
137: }
138: }
139:
140: private class VariableEditor extends StandardDialog {
141: /**
142: *
143: */
144: private static final long serialVersionUID = 1L;
145: private JTextField varNameField = new JTextField();
146: private JTextArea textArea = new JTextArea();
147: private String title = i18n.str("variable_editor"); //$NON-NLS-1$
148:
149: public VariableEditor(Dialog d) {
150: super (d, "");
151: init();
152: }
153:
154: public VariableEditor(Frame f) {
155: super (f, "");
156: init();
157: }
158:
159: public VariableEditor() {
160: init();
161: }
162:
163: private void init() {
164: setTitle(title);
165:
166: TextEditPopupManager popper = TextEditPopupManager
167: .getInstance();
168: popper.registerJTextComponent(varNameField);
169: popper.registerJTextComponent(textArea);
170:
171: JPanel namePanel = new JPanel(new BorderLayout());
172: namePanel.add(
173: new JLabel(i18n.str("name")), BorderLayout.NORTH); //$NON-NLS-1$
174: namePanel.add(varNameField, BorderLayout.CENTER);
175:
176: JPanel valuePanel = new JPanel(new BorderLayout());
177: valuePanel.add(
178: new JLabel(i18n.str("value")), BorderLayout.NORTH); //$NON-NLS-1$
179: valuePanel.add(new JScrollPane(textArea),
180: BorderLayout.CENTER);
181:
182: JPanel mainPanel = new JPanel(new BorderLayout(5, 5));
183: mainPanel.add(namePanel, BorderLayout.NORTH);
184: mainPanel.add(valuePanel, BorderLayout.CENTER);
185:
186: setContentPane(mainPanel);
187: setSize(360, 250);
188: }
189:
190: public void setVariable(CustomTag v) {
191: varNameField.setText(v.getName());
192: textArea.setText(v.getValue());
193: }
194:
195: public void setVariable(String name, String value) {
196: varNameField.setText(name);
197: textArea.setText(value);
198: }
199:
200: public CustomTag getVariable() {
201: String name = varNameField.getText();
202: String value = textArea.getText();
203: return new CustomTag(name, value);
204: }
205:
206: public boolean isValidData() {
207: String s = varNameField.getText();
208: if (s.length() == 0) {
209: JOptionPane
210: .showMessageDialog(
211: VariableEditor.this ,
212: i18n.str("no_name_prompt"), //$NON-NLS-1$
213: i18n.str("invalid_variable"), JOptionPane.WARNING_MESSAGE); //$NON-NLS-1$
214: return false;
215: }
216:
217: if (textArea.getText().length() == 0) {
218: JOptionPane
219: .showMessageDialog(
220: VariableEditor.this ,
221: i18n.str("no_value_prompt"), //$NON-NLS-1$
222: i18n.str("invalid_variable"), JOptionPane.WARNING_MESSAGE); //$NON-NLS-1$
223: return false;
224: }
225:
226: for (int i = 0; i < s.length(); i++) {
227: char ch = s.charAt(i);
228: if (!Character.isJavaIdentifierPart(ch) || ch == '$') {
229: JOptionPane
230: .showMessageDialog(
231: VariableEditor.this ,
232: i18n
233: .str("invalid_variable_name_prompt"), //$NON-NLS-1$
234: i18n.str("invalid_variable"), JOptionPane.WARNING_MESSAGE); //$NON-NLS-1$
235:
236: return false;
237: }
238: }
239:
240: return true;
241: }
242: }
243: }
|