001: /*
002: * Gruntspud
003: *
004: * Copyright (C) 2002 Brett Smith.
005: *
006: * Written by: Brett Smith <t_magicthize@users.sourceforge.net>
007: *
008: * This program is free software; you can redistribute it and/or modify it under the terms of the GNU Library General Public
009: * License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.
010: * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License for more details.
012: *
013: * You should have received a copy of the GNU Library General Public License along with this program; if not, write to the Free
014: * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
015: */
016:
017: package gruntspud.actions;
018:
019: import gruntspud.CVSCommandHandler;
020: import gruntspud.CVSFileNode;
021: import gruntspud.CVSSubstType;
022: import gruntspud.Constants;
023: import gruntspud.GruntspudContext;
024: import gruntspud.GruntspudUtil;
025: import gruntspud.ResourceUtil;
026: import gruntspud.ui.OptionDialog;
027: import gruntspud.ui.SubstTypeListCellRenderer;
028: import gruntspud.ui.SubstTypeListModel;
029: import gruntspud.ui.UIUtil;
030: import gruntspud.ui.XTextField;
031: import gruntspud.ui.view.ViewManager;
032:
033: import java.awt.BorderLayout;
034: import java.awt.Component;
035: import java.awt.FlowLayout;
036: import java.awt.Font;
037: import java.awt.GridBagConstraints;
038: import java.awt.GridBagLayout;
039: import java.awt.Insets;
040: import java.awt.event.ActionEvent;
041: import java.awt.event.ActionListener;
042: import java.io.File;
043: import java.io.FileOutputStream;
044: import java.io.IOException;
045: import java.util.ResourceBundle;
046:
047: import javax.swing.BorderFactory;
048: import javax.swing.JCheckBox;
049: import javax.swing.JComboBox;
050: import javax.swing.JLabel;
051: import javax.swing.JOptionPane;
052: import javax.swing.JPanel;
053: import javax.swing.UIManager;
054:
055: import org.netbeans.lib.cvsclient.command.Command;
056: import org.netbeans.lib.cvsclient.command.add.AddCommand;
057:
058: /**
059: * Action to create a new file in the selected directory. The user is given the choice to add the file to CVS and / or open it in
060: * the default editor.
061: *
062: * @author magicthize @created 26 May 2002
063: */
064: public class NewFileAction extends AddAction {
065: static ResourceBundle res = ResourceBundle
066: .getBundle("gruntspud.actions.ResourceBundle");
067:
068: /**
069: * Constructor for the NewFileAction object
070: *
071: * @param context context
072: */
073: public NewFileAction(GruntspudContext context) {
074: super (res, "newFileAction", context);
075: putValue(GruntspudAction.ICON, UIUtil
076: .getCachedIcon(Constants.ICON_TOOL_NEW_FILE));
077: putValue(DefaultGruntspudAction.SMALL_ICON, UIUtil
078: .getCachedIcon(Constants.ICON_TOOL_SMALL_NEW_FILE));
079: setUpdatesFiles(true);
080: }
081:
082: /*
083: * (non-Javadoc)
084: *
085: * @see gruntspud.actions.GruntspudAction#checkAvailable()
086: */
087: public boolean checkAvailable() {
088: ViewManager mg = getContext().getViewManager();
089: CVSFileNode[] sel = mg.getSelectedNodes();
090:
091: return !CVSCommandHandler.getInstance().isCommandRunning()
092: && mg.isHomeExists()
093: && ((sel == null) || ((sel.length < 2) && (mg
094: .getSelectedFileCount() == 0)));
095: }
096:
097: /*
098: * (non-Javadoc)
099: *
100: * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
101: */
102: public void actionPerformed(final ActionEvent evt) {
103: final CVSFileNode sel = getContext().getViewManager()
104: .getNodesToPerformActionOn()[0];
105: AddFileOptionsPane opts = new AddFileOptionsPane(sel);
106: final OptionDialog.Option ok = new OptionDialog.Option(
107: res
108: .getString("newFileAction.optionDialog.option.ok.text"),
109: res
110: .getString("newFileAction.optionDialog.option.ok.toolTipText"),
111: ResourceUtil
112: .getResourceMnemonic(res,
113: "newFileAction.optionDialog.option.ok.mnemonic"));
114: OptionDialog.Option cancel = new OptionDialog.Option(
115: res
116: .getString("newFileAction.optionDialog.option.cancel.text"),
117: res
118: .getString("newFileAction.optionDialog.option.cancel.toolTipText"),
119: ResourceUtil
120: .getResourceMnemonic(res,
121: "newFileAction.optionDialog.option.cancel.mnemonic"));
122: Component parent = getParentComponentForEvent(evt);
123: OptionDialog.Option opt = OptionDialog.showOptionDialog(
124: "newFile", getContext(), parent,
125: new OptionDialog.Option[] { ok, cancel }, opts, res
126: .getString("newFileAction.optionDialog.title"),
127: ok, new OptionDialog.Callback() {
128: public boolean canClose(OptionDialog dialog,
129: OptionDialog.Option option) {
130: return true;
131: }
132: }
133:
134: , false, true);
135:
136: if ((opt != ok) || opts.getFileName().equals("")) {
137: return;
138: }
139:
140: File f = new File(sel.getFile(), opts.getFileName());
141:
142: if (f.exists()
143: && (JOptionPane
144: .showConfirmDialog(
145: parent,
146: res
147: .getString("newFileAction.warning.fileExists.text"),
148: res
149: .getString("newFileAction.warning.fileExists.title"),
150: JOptionPane.YES_NO_OPTION,
151: JOptionPane.WARNING_MESSAGE) == JOptionPane.NO_OPTION)) {
152: return;
153: }
154:
155: FileOutputStream out = null;
156:
157: try {
158: out = new FileOutputStream(f);
159: } catch (IOException ioe) {
160: GruntspudUtil
161: .showErrorMessage(
162: getContext().getHost().getMainComponent(),
163: res
164: .getString("newFileAction.error.fileCouldNotBeCreated.text"),
165: res
166: .getString("newFileAction.error.fileCouldNotBeCreated.title"),
167: ioe);
168: } finally {
169: GruntspudUtil.closeStream(out);
170: }
171:
172: getContext().getViewManager().fileUpdated(f);
173: getContext().getViewManager().updateNodes();
174:
175: final CVSFileNode newNode = getContext().getViewManager()
176: .findNodeForPath(sel, f, true);
177:
178: if (newNode != null) {
179: if (opts.isAddToCVS()) {
180: final AddCommand cmd = new AddCommand();
181: CVSCommandHandler.getInstance()
182: .runCommandGroup(parent, getContext(), null,
183: new Command[] { cmd },
184: new CVSFileNode[] { newNode },
185: new CVSSubstType[] { opts.getType() },
186: false, null, null, this ,
187: getEnabledOptionalListeners());
188: }
189:
190: if (opts.isEditFile()) {
191: try {
192: getContext().getHost().openNode(newNode);
193: } catch (IOException ioe) {
194: GruntspudUtil
195: .showErrorMessage(
196: getContext().getHost()
197: .getMainComponent(),
198: res
199: .getString("newFileAction.error.fileCouldNotBeOpened.text"),
200: res
201: .getString("newFileAction.error.fileCouldNotBeOpened.title"),
202: ioe);
203: }
204: }
205: }
206:
207: }
208:
209: class AddFileOptionsPane extends JPanel {
210: private JCheckBox addToCVS;
211: private JCheckBox editFile;
212: private JComboBox type;
213: private XTextField fileName;
214: private CVSFileNode[] sel;
215:
216: AddFileOptionsPane(CVSFileNode sel) {
217: super (new BorderLayout());
218:
219: JPanel t = new JPanel(new FlowLayout(FlowLayout.CENTER, 2,
220: 2));
221: t.add(new JLabel(res
222: .getString("newFileAction.createIn.text")));
223:
224: JLabel x = new JLabel(sel.getName());
225: x.setToolTipText(sel.getFile().getAbsolutePath());
226: x.setFont(UIManager.getFont("Label.font").deriveFont(
227: Font.BOLD));
228: t.add(x);
229:
230: JPanel n = new JPanel(new FlowLayout(FlowLayout.CENTER, 2,
231: 2));
232: fileName = new XTextField(25);
233: fileName.grabFocus();
234: n.add(new JLabel(res.getString("newFileAction.name.text")));
235: n.add(fileName);
236:
237: JPanel e = new JPanel(new BorderLayout());
238: e.setBorder(BorderFactory.createEmptyBorder(4, 8, 4, 8));
239: e.add(n, BorderLayout.CENTER);
240: e.add(t, BorderLayout.NORTH);
241:
242: JPanel g = new JPanel(new GridBagLayout());
243: g.setBorder(BorderFactory.createEmptyBorder(4, 8, 4, 8));
244:
245: GridBagConstraints gbc = new GridBagConstraints();
246: gbc.insets = new Insets(1, 1, 1, 1);
247: gbc.anchor = GridBagConstraints.NORTHWEST;
248: gbc.fill = GridBagConstraints.HORIZONTAL;
249: gbc.weightx = 1.0;
250: editFile = new JCheckBox(res
251: .getString("newFileAction.editFile.text"));
252: UIUtil.jGridBagAdd(g, editFile, gbc,
253: GridBagConstraints.REMAINDER);
254: e.add(g, BorderLayout.SOUTH);
255:
256: if ((sel != null) && (sel.getCVSRoot() != null)) {
257: addToCVS = new JCheckBox(res
258: .getString("newFileAction.addToCVS.text"));
259: addToCVS.setBorder(BorderFactory.createEmptyBorder(8,
260: 8, 8, 8));
261: type = new JComboBox(
262: new SubstTypeListModel(
263: new CVSSubstType[] {
264: CVSSubstType.CVS_SUBST_TYPE_TEXT,
265: CVSSubstType.CVS_SUBST_TYPE_DEFAULT_LOCKER,
266: CVSSubstType.CVS_SUBST_TYPE_OLD_VALUES,
267: CVSSubstType.CVS_SUBST_TYPE_ONLY_KEYWORDS,
268: CVSSubstType.CVS_SUBST_TYPE_ONLY_VALUES }));
269: addToCVS.addActionListener(new ActionListener() {
270: public void actionPerformed(ActionEvent evt) {
271: type.setEnabled(addToCVS.isSelected());
272: }
273: });
274: type.setRenderer(new SubstTypeListCellRenderer());
275: type.setEnabled(false);
276: UIUtil.jGridBagAdd(g, addToCVS, gbc,
277: GridBagConstraints.REMAINDER);
278: gbc.insets.left = 24;
279: UIUtil.jGridBagAdd(g, type, gbc,
280: GridBagConstraints.REMAINDER);
281: gbc.insets.left = 1;
282: }
283:
284: add(
285: new JLabel(
286: UIUtil
287: .getCachedIcon(Constants.ICON_TOOL_LARGE_NEW_FILE)),
288: BorderLayout.WEST);
289: add(e, BorderLayout.CENTER);
290: }
291:
292: private boolean isAddToCVS() {
293: return addToCVS.isSelected();
294: }
295:
296: public boolean isEditFile() {
297: return editFile.isSelected();
298: }
299:
300: public CVSSubstType getType() {
301: return (CVSSubstType) type.getSelectedItem();
302: }
303:
304: public String getFileName() {
305: return fileName.getText();
306: }
307: }
308: }
|