001: package net.sf.thingamablog.gui.app;
002:
003: import java.awt.Dimension;
004: import java.awt.GridBagConstraints;
005: import java.awt.GridBagLayout;
006: import java.awt.Insets;
007: import java.awt.event.ActionEvent;
008: import java.awt.event.ActionListener;
009: import javax.swing.JButton;
010: import javax.swing.JLabel;
011: import javax.swing.JList;
012: import javax.swing.JOptionPane;
013: import javax.swing.JPanel;
014: import javax.swing.JScrollPane;
015: import javax.swing.ListSelectionModel;
016:
017: import net.atlanticbb.tantlinger.i18n.I18n;
018:
019: public class SelectTemplatePanel extends JPanel {
020: /**
021: *
022: */
023: private static final long serialVersionUID = 1L;
024: private static final I18n i18n = I18n
025: .getInstance("net.sf.thingamablog.gui.app");
026: public static final String TEMPLATE_ZIP_PATH = "templateZip"; // @jve:decl-index=0: //$NON-NLS-1$
027: public static final String TEMPLATE_NAME = "templateName"; //$NON-NLS-1$
028:
029: private JLabel msgLabel = null;
030: private JButton openButton = null;
031: private JScrollPane scrollPane = null;
032: private JList tmplList = null;
033:
034: private String zipPath;
035:
036: /**
037: * This method initializes
038: *
039: */
040: public SelectTemplatePanel() {
041: super ();
042: initialize();
043: }
044:
045: public boolean canProceed() {
046: if (zipPath == null) {
047: JOptionPane
048: .showMessageDialog(
049: this ,
050: i18n.str("open_pack_prompt"), i18n.str("missing_data"), JOptionPane.WARNING_MESSAGE); //$NON-NLS-1$ //$NON-NLS-2$
051: return false;
052: }
053:
054: //values.put(TEMPLATE_ZIP_PATH, zipPath);
055:
056: String tName = (String) tmplList.getSelectedValue();
057: if (tName == null) {
058: JOptionPane
059: .showMessageDialog(
060: this ,
061: i18n.str("open_pack_prompt"), i18n.str("missing_data"), JOptionPane.WARNING_MESSAGE); //$NON-NLS-1$ //$NON-NLS-2$
062: return false;
063: }
064:
065: //values.put(TEMPLATE_NAME, tName);
066:
067: return true;
068: }
069:
070: /**
071: * This method initializes this
072: *
073: */
074: private void initialize() {
075: GridBagConstraints gridBagConstraints2 = new GridBagConstraints();
076: gridBagConstraints2.fill = GridBagConstraints.BOTH;
077: gridBagConstraints2.gridy = 1;
078: gridBagConstraints2.weightx = 1.0;
079: gridBagConstraints2.weighty = 1.0;
080: gridBagConstraints2.insets = new Insets(5, 0, 0, 0);
081: gridBagConstraints2.gridx = 1;
082: GridBagConstraints gridBagConstraints1 = new GridBagConstraints();
083: gridBagConstraints1.gridx = 0;
084: gridBagConstraints1.anchor = GridBagConstraints.NORTHWEST;
085: gridBagConstraints1.insets = new Insets(5, 0, 0, 5);
086: gridBagConstraints1.gridy = 1;
087: GridBagConstraints gridBagConstraints = new GridBagConstraints();
088: gridBagConstraints.gridx = 0;
089: gridBagConstraints.anchor = GridBagConstraints.NORTHWEST;
090: gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
091: gridBagConstraints.weightx = 1.0;
092: gridBagConstraints.gridwidth = 2;
093: gridBagConstraints.gridy = 0;
094: msgLabel = new JLabel();
095: msgLabel
096: .setText("<html>" + i18n.str("select_template_prompt") + "</html>"); //$NON-NLS-1$
097: this .setLayout(new GridBagLayout());
098: this .setSize(new Dimension(434, 260));
099: this .add(msgLabel, gridBagConstraints);
100: this .add(getOpenButton(), gridBagConstraints1);
101: this .add(getScrollPane(), gridBagConstraints2);
102:
103: }
104:
105: /**
106: * This method initializes openButton
107: *
108: * @return javax.swing.JButton
109: */
110: private JButton getOpenButton() {
111: if (openButton == null) {
112: openButton = new JButton();
113: openButton.setText(i18n.str("open_zip_file_")); //$NON-NLS-1$
114: openButton.addActionListener(new OpenHandler());
115: }
116: return openButton;
117: }
118:
119: /**
120: * This method initializes scrollPane
121: *
122: * @return javax.swing.JScrollPane
123: */
124: private JScrollPane getScrollPane() {
125: if (scrollPane == null) {
126: scrollPane = new JScrollPane();
127: scrollPane.setViewportView(getTmplList());
128: }
129: return scrollPane;
130: }
131:
132: /**
133: * This method initializes tmplList
134: *
135: * @return javax.swing.JList
136: */
137: private JList getTmplList() {
138: if (tmplList == null) {
139: tmplList = new JList();
140: tmplList.getSelectionModel().setSelectionMode(
141: ListSelectionModel.SINGLE_SELECTION);
142: }
143: return tmplList;
144: }
145:
146: /*
147: private String getTemplateDirRoot(String entPath)
148: {
149: int pos = entPath.lastIndexOf("/templates/"); //$NON-NLS-1$
150: if(pos == -1)
151: return null;
152: String root = entPath.substring(0, pos);
153:
154: int slashCount = 0;
155: for(int i = 0; i < root.length(); i++)
156: {
157: if(root.charAt(i) == '/')
158: slashCount++;
159: if(slashCount > 2)
160: return null;
161: }
162: return root;
163: }
164: */
165:
166: private class OpenHandler implements ActionListener {
167: public void actionPerformed(ActionEvent e) {
168: /*JFileChooser fc = new JFileChooser();
169: fc.setFileSelectionMode(JFileChooser.FILES_ONLY);
170: fc.setDialogTitle("Open Template Pack");
171: fc.setFileFilter(new javax.swing.filechooser.FileFilter()
172: {
173: public boolean accept(File f)
174: {
175: return f.isDirectory() ||
176: f.getName().toLowerCase().endsWith(".zip");
177: }
178:
179: public String getDescription()
180: {
181: return "Template Pack Zip file";
182: }
183: });
184: String lastTmplPack = (String)TemplateImportWizard.getProperties().get("last_tmpl_pack");
185: if(lastTmplPack != null)
186: {
187: File f = new File(lastTmplPack);
188: if(f.exists())
189: fc.setSelectedFile(f);
190: }
191:
192: int r = fc.showOpenDialog(SelectTemplatePanel.this);
193: if(r == JFileChooser.CANCEL_OPTION || fc.getSelectedFile() == null)
194: return;
195:
196: File zipFile = fc.getSelectedFile();
197: ZipFile z = null;
198:
199: try
200: {
201: z = new ZipFile(zipFile);
202: zipPath = zipFile.getAbsolutePath();
203: Enumeration eenum = z.entries();
204: Vector roots = new Vector();
205:
206: while(eenum.hasMoreElements())
207: {
208: ZipEntry ze = (ZipEntry) eenum.nextElement();
209: String name = ze.getName();
210: if(name.toLowerCase().endsWith(".template"))
211: {
212: String root = getTemplateDirRoot(name);
213: if(root != null && !roots.contains(root))
214: {
215: //System.err.println(root);
216: roots.add(root);
217: }
218: }
219: }
220:
221: getTmplList().setListData(roots);
222: //TemplateImportWizard.getProperties().put("last_tmpl_pack", zipFile.getAbsolutePath());
223:
224: }
225: catch(Exception ex)
226: {
227: ex.printStackTrace();
228: }
229: finally
230: {
231: if(z != null)
232: {
233: try
234: {
235: z.close();
236: }
237: catch(Exception ex){}
238: }
239: } */
240: }
241: }
242:
243: } // @jve:decl-index=0:visual-constraint="10,10"
|