001: /*
002: * Created on Oct 31, 2007
003: */
004: package net.sf.thingamablog.gui.app;
005:
006: import java.awt.Dialog;
007: import java.awt.Dimension;
008: import java.awt.Frame;
009: import java.awt.GridBagConstraints;
010: import java.awt.GridBagLayout;
011: import java.awt.Insets;
012: import java.awt.event.ActionEvent;
013: import java.awt.event.ActionListener;
014: import java.awt.event.KeyEvent;
015: import java.io.File;
016: import java.io.FileInputStream;
017: import java.io.IOException;
018: import java.io.InputStream;
019: import java.text.SimpleDateFormat;
020: import java.util.ArrayList;
021: import java.util.Arrays;
022: import java.util.Date;
023: import java.util.List;
024: import java.util.Properties;
025: import java.util.Stack;
026:
027: import javax.swing.BorderFactory;
028: import javax.swing.JButton;
029: import javax.swing.JFileChooser;
030: import javax.swing.JLabel;
031: import javax.swing.JOptionPane;
032: import javax.swing.JPanel;
033: import javax.swing.JScrollPane;
034: import javax.swing.JTabbedPane;
035: import javax.swing.JTextField;
036: import javax.swing.JTree;
037: import javax.swing.SwingConstants;
038: import javax.swing.tree.TreeModel;
039: import javax.swing.tree.TreePath;
040:
041: import net.atlanticbb.tantlinger.i18n.I18n;
042: import net.atlanticbb.tantlinger.io.IOUtils;
043: import net.atlanticbb.tantlinger.ui.UIUtils;
044: import net.atlanticbb.tantlinger.ui.text.TextEditPopupManager;
045: import net.sf.thingamablog.TBGlobals;
046: import net.sf.thingamablog.blog.TBWeblog;
047: import net.sf.thingamablog.blog.TemplatePack;
048: import net.sf.thingamablog.blog.ZipExportableTemplatePack;
049: import net.sf.thingamablog.gui.StandardDialog;
050:
051: /**
052: * @author Bob Tantlinger
053: *
054: */
055: public class ExportTemplatePackDialog extends StandardDialog {
056: /**
057: *
058: */
059: private static final long serialVersionUID = 1L;
060: private static final I18n i18n = I18n
061: .getInstance("net.sf.thingamablog.gui.app"); //$NON-NLS-1$
062: private static final String TITLE = i18n.str("export_templates"); //$NON-NLS-1$
063:
064: private JLabel msgLabel = null;
065: private JLabel backupNameLabel = null;
066: private JTextField backupNameField = null;
067: private JScrollPane scrollPane = null;
068: private JTree fileTree = null;
069: private JLabel locLabel = null;
070: private JTextField locField = null;
071: private JButton browseButton = null;
072: private JLabel filesLabel = null;
073: private CheckTreeManager checkTreeManager = null;
074:
075: private TemplatePropertiesPanel propertiesPanel;
076:
077: private File backupDir;
078: private TBWeblog weblog;
079:
080: /**
081: * @param parent
082: * @param title
083: */
084: public ExportTemplatePackDialog(Frame parent, TBWeblog blog) {
085: super (parent, TITLE, BUTTONS_RIGHT, 5);
086: weblog = blog;
087: initialize();
088: }
089:
090: /**
091: * @param parent
092: * @param title
093: */
094: public ExportTemplatePackDialog(Dialog parent, TBWeblog blog) {
095: super (parent, TITLE, BUTTONS_RIGHT, 5);
096: weblog = blog;
097: initialize();
098: }
099:
100: public boolean isValidData() {
101: if (backupDir == null || backupNameField.equals("")) //$NON-NLS-1$
102: {
103: UIUtils.showWarning(this , i18n
104: .str("specifiy_template_export_location")); //$NON-NLS-1$
105: return false;
106: }
107:
108: //get the web files...
109: List paths = getAllCheckedPaths(checkTreeManager, fileTree);
110: List fileList = new ArrayList();
111: if (paths != null) {
112: for (int i = 0; i < paths.size(); i++) {
113: TreePath p = (TreePath) paths.get(i);
114: File f = (File) p.getLastPathComponent();
115: if (!f.isDirectory())
116: fileList.add(f);
117: }
118: }
119:
120: File dir = weblog.getHomeDirectory();
121: String fileName = backupNameField.getText();
122: if (!fileName.toLowerCase().endsWith(".zip")) //$NON-NLS-1$
123: fileName = fileName + ".zip"; //$NON-NLS-1$
124:
125: //check if the file already exists and prompt for overwrite
126: File outFile = new File(backupDir, fileName);
127: if (outFile.exists()) {
128: int yn = JOptionPane.showConfirmDialog(this , i18n
129: .str("overwrite") + "[" + outFile.getName() + "]", //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
130: i18n.str("confirm"), //$NON-NLS-1$
131: JOptionPane.YES_NO_OPTION);
132:
133: if (yn == JOptionPane.NO_OPTION)
134: return false;
135: }
136:
137: File[] webFiles = (File[]) fileList.toArray(new File[fileList
138: .size()]);
139: Properties props = propertiesPanel.getProperties();
140:
141: //TODO this shoud be done in its own thread...
142: try {
143: TemplatePack zipPack = new ZipExportableTemplatePack(dir,
144: fileName, webFiles);
145: zipPack.getPackProperties().putAll(props);
146: zipPack.installPack(backupDir);
147: UIUtils.showInfo(this , i18n
148: .str("exported_templates_prompt")); //$NON-NLS-1$
149: }
150: /*catch(IllegalArgumentException e)
151: {
152: UIUtils.showError(this, e);
153: }*/
154: catch (Exception e) {
155: UIUtils.showError(this , e);
156: }
157:
158: return true;
159: }
160:
161: /**
162: * This method initializes this
163: *
164: */
165: private void initialize() {
166: GridBagConstraints gridBagConstraints4 = new GridBagConstraints();
167: gridBagConstraints4.gridx = 1;
168: gridBagConstraints4.anchor = GridBagConstraints.NORTHEAST;
169: gridBagConstraints4.insets = new Insets(0, 0, 0, 5);
170: gridBagConstraints4.gridy = 3;
171: filesLabel = new JLabel();
172: filesLabel.setText(i18n.str("web_files")); //$NON-NLS-1$
173: GridBagConstraints gridBagConstraints31 = new GridBagConstraints();
174: gridBagConstraints31.gridx = 3;
175: gridBagConstraints31.anchor = GridBagConstraints.NORTHWEST;
176: gridBagConstraints31.gridy = 1;
177: GridBagConstraints gridBagConstraints21 = new GridBagConstraints();
178: gridBagConstraints21.fill = GridBagConstraints.HORIZONTAL;
179: gridBagConstraints21.gridy = 1;
180: gridBagConstraints21.weightx = 1.0;
181: gridBagConstraints21.anchor = GridBagConstraints.WEST;
182: gridBagConstraints21.insets = new Insets(0, 0, 5, 3);
183: gridBagConstraints21.gridx = 2;
184: GridBagConstraints gridBagConstraints11 = new GridBagConstraints();
185: gridBagConstraints11.gridx = 1;
186: gridBagConstraints11.anchor = GridBagConstraints.EAST;
187: gridBagConstraints11.insets = new Insets(0, 0, 5, 5);
188: gridBagConstraints11.gridy = 1;
189: locLabel = new JLabel();
190: locLabel.setText(i18n.str("create_in_folder")); //$NON-NLS-1$
191: GridBagConstraints gridBagConstraints3 = new GridBagConstraints();
192: gridBagConstraints3.fill = GridBagConstraints.BOTH;
193: gridBagConstraints3.gridy = 3;
194: gridBagConstraints3.weightx = 1.0;
195: gridBagConstraints3.weighty = 1.0;
196: gridBagConstraints3.gridwidth = 2;
197: gridBagConstraints3.gridx = 2;
198: GridBagConstraints gridBagConstraints2 = new GridBagConstraints();
199: gridBagConstraints2.fill = GridBagConstraints.HORIZONTAL;
200: gridBagConstraints2.gridy = 2;
201: gridBagConstraints2.weightx = 1.0;
202: gridBagConstraints2.anchor = GridBagConstraints.WEST;
203: gridBagConstraints2.insets = new Insets(0, 0, 5, 0);
204: gridBagConstraints2.gridwidth = 2;
205: gridBagConstraints2.gridx = 2;
206: GridBagConstraints gridBagConstraints1 = new GridBagConstraints();
207: gridBagConstraints1.gridx = 1;
208: gridBagConstraints1.anchor = GridBagConstraints.EAST;
209: gridBagConstraints1.insets = new Insets(0, 0, 5, 5);
210: gridBagConstraints1.gridy = 2;
211: backupNameLabel = new JLabel();
212: backupNameLabel.setText(i18n.str("file_name")); //$NON-NLS-1$
213: GridBagConstraints gridBagConstraints = new GridBagConstraints();
214: gridBagConstraints.gridx = 1;
215: gridBagConstraints.anchor = GridBagConstraints.NORTHWEST;
216: gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
217: gridBagConstraints.weightx = 1.0;
218: gridBagConstraints.gridwidth = 3;
219: gridBagConstraints.insets = new Insets(0, 0, 10, 0);
220: gridBagConstraints.gridy = 0;
221: msgLabel = new JLabel();
222: msgLabel
223: .setText("<html>" + i18n.str("export_templates_prompt") + "</html>"); //$NON-NLS-1$
224:
225: JPanel exportPanel = new JPanel();
226: exportPanel.setLayout(new GridBagLayout());
227: exportPanel.setSize(new Dimension(497, 251));
228: exportPanel.add(msgLabel, gridBagConstraints);
229: exportPanel.add(backupNameLabel, gridBagConstraints1);
230: exportPanel.add(getBackupNameField(), gridBagConstraints2);
231: exportPanel.add(getScrollPane(), gridBagConstraints3);
232: exportPanel.add(locLabel, gridBagConstraints11);
233: exportPanel.add(getLocField(), gridBagConstraints21);
234: exportPanel.add(getBrowseButton(), gridBagConstraints31);
235: exportPanel.add(filesLabel, gridBagConstraints4);
236: exportPanel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5,
237: 5));
238:
239: checkTreeManager = new CheckTreeManager(getFileTree());
240:
241: JTabbedPane tabs = new JTabbedPane(SwingConstants.TOP);
242: tabs.addTab(i18n.str("export"), exportPanel); //$NON-NLS-1$
243:
244: propertiesPanel = new TemplatePropertiesPanel();
245: tabs.addTab(i18n.str("properties"), propertiesPanel); //$NON-NLS-1$
246:
247: this .setContentPane(tabs);
248: setSize(425, 340);
249: setResizable(false);
250:
251: Properties p = new Properties();
252: InputStream in = null;
253: try {
254: in = new FileInputStream(new File(
255: weblog.getHomeDirectory(), "pack.properties")); //$NON-NLS-1$
256: p.load(in);
257: } catch (IOException ex) {
258: ex.printStackTrace();
259: } finally {
260: IOUtils.close(in);
261: }
262:
263: p.setProperty("created", new Date().getTime() + ""); //$NON-NLS-1$ //$NON-NLS-2$
264: propertiesPanel.setProperties(p);
265: updateTree();
266: }
267:
268: /**
269: * This method initializes backupNameField
270: *
271: * @return javax.swing.JTextField
272: */
273: private JTextField getBackupNameField() {
274: if (backupNameField == null) {
275: backupNameField = new JTextField();
276: String name = IOUtils.sanitize(weblog.getTitle() + "-"); //$NON-NLS-1$
277: SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); //$NON-NLS-1$
278: name += "templates-" + sdf.format(new Date()) + ".zip"; //$NON-NLS-1$ //$NON-NLS-2$
279: backupNameField.setText(name);
280: TextEditPopupManager.getInstance().registerJTextComponent(
281: backupNameField);
282: }
283: return backupNameField;
284: }
285:
286: /**
287: * This method initializes scrollPane
288: *
289: * @return javax.swing.JScrollPane
290: */
291: private JScrollPane getScrollPane() {
292: if (scrollPane == null) {
293: scrollPane = new JScrollPane();
294: scrollPane.setViewportView(getFileTree());
295: }
296: return scrollPane;
297: }
298:
299: /**
300: * This method initializes fileTree
301: *
302: * @return javax.swing.JTree
303: */
304: private JTree getFileTree() {
305: if (fileTree == null) {
306: //String dir = "C:\\Documents and Settings\\Owner\\My Documents\\weblogs\\1092845426875\\web";
307: TreeModel model = new FileTreeModel(new File("web")); //$NON-NLS-1$
308: fileTree = new JTree(model);
309: fileTree.setCellRenderer(new FileTreeCellRenderer());
310: }
311: return fileTree;
312: }
313:
314: /**
315: * This method initializes locField
316: *
317: * @return javax.swing.JTextField
318: */
319: private JTextField getLocField() {
320: if (locField == null) {
321: locField = new JTextField();
322: locField.setEditable(false);
323: TextEditPopupManager.getInstance().registerJTextComponent(
324: locField);
325: }
326: return locField;
327: }
328:
329: /**
330: * This method initializes browseButton
331: *
332: * @return javax.swing.JButton
333: */
334: private JButton getBrowseButton() {
335: if (browseButton == null) {
336: browseButton = new JButton();
337: browseButton.setMargin(new Insets(0, 0, 0, 0));
338: browseButton.setText(""); //$NON-NLS-1$
339: browseButton.setToolTipText(i18n.str("browse_")); //$NON-NLS-1$
340: browseButton.setMnemonic(KeyEvent.VK_UNDEFINED);
341: browseButton.setIcon(UIUtils.getIcon(UIUtils.X16,
342: "import.png")); //$NON-NLS-1$
343: browseButton.setPreferredSize(new Dimension(21, 21));
344: browseButton.addActionListener(new BrowseHandler());
345: }
346: return browseButton;
347: }
348:
349: private void updateTree() {
350: //String userxml = (String)values.get(SelectBlogPanel.USERXML_PATH_PROPERTY);
351: //String bkey = (String)values.get(SelectBlogPanel.BLOGKEY_PROPERTY);
352: //if(userxml != null && bkey != null)
353: {
354: //File f = new File(userxml);
355: File webRoot = weblog.getHomeDirectory();//new File(f.getParentFile(), bkey);
356: webRoot = new File(webRoot, "web"); //$NON-NLS-1$
357:
358: //System.err.println(webRoot + " " + webRoot.exists());
359:
360: FileTreeModel m = (FileTreeModel) fileTree.getModel();
361: if (!m.getRoot().equals(webRoot)) {
362: //got to unselect the paths in the selecton model, otherwise
363: //they stay selected and are not part of the new TreeModel
364: List paths = getAllCheckedPaths(checkTreeManager,
365: fileTree);
366: for (int i = 0; i < paths.size(); i++) {
367: TreePath p = (TreePath) paths.get(i);
368: checkTreeManager.getSelectionModel()
369: .removeSelectionPath(p);
370: }
371: fileTree.setModel(new FileTreeModel(webRoot));
372: }
373: }
374: }
375:
376: private void addChildPaths(TreePath path, TreeModel model,
377: List result) {
378: Object item = path.getLastPathComponent();
379: int childCount = model.getChildCount(item);
380: for (int i = 0; i < childCount; i++)
381: result.add(path.pathByAddingChild(model.getChild(item, i)));
382: }
383:
384: private ArrayList getDescendants(TreePath paths[], TreeModel model) {
385: ArrayList result = new ArrayList();
386: Stack pending = new Stack();
387: pending.addAll(Arrays.asList(paths));
388: while (!pending.isEmpty()) {
389: TreePath path = (TreePath) pending.pop();
390: addChildPaths(path, model, pending);
391: result.add(path);
392: }
393: return result;
394: }
395:
396: private ArrayList getAllCheckedPaths(CheckTreeManager manager,
397: JTree tree) {
398: TreePath p[] = manager.getSelectionModel().getSelectionPaths();
399: if (p == null)
400: return new ArrayList();
401: return getDescendants(p, tree.getModel());
402: }
403:
404: private class BrowseHandler implements ActionListener {
405: public void actionPerformed(ActionEvent e) {
406: JFileChooser fc = new JFileChooser();
407: fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
408: fc.setDialogTitle(i18n.str("select_backup_folder")); //$NON-NLS-1$
409: String lastDir = TBGlobals.getProperty("last_backup_dir"); //$NON-NLS-1$
410: if (lastDir != null) {
411: File f = new File(lastDir);
412: if (f.exists()) {
413: fc.setSelectedFile(f);
414: }
415: }
416:
417: int r = fc.showDialog(ExportTemplatePackDialog.this , "OK"); //$NON-NLS-1$
418:
419: //int r = fc.showOpenDialog(BackUpPanel.this);
420: if (r == JFileChooser.CANCEL_OPTION
421: || fc.getSelectedFile() == null)
422: return;
423:
424: backupDir = fc.getSelectedFile();
425: TBGlobals.putProperty(
426: "last_backup_dir", backupDir.getAbsolutePath()); //$NON-NLS-1$
427: locField.setText(backupDir.getAbsolutePath());
428: }
429: }
430: }
|