OSDN Git Service

プラットフォームごとのStigmataのホームディレクトリの場所を返す一連のクラスをリファクタリングした.
[stigmata/stigmata.git] / src / main / java / jp / sourceforge / stigmata / ui / swing / UpdatePluginsPane.java
1 package jp.sourceforge.stigmata.ui.swing;
2
3 import java.awt.BorderLayout;
4 import java.awt.Component;
5 import java.awt.GridBagConstraints;
6 import java.awt.GridBagLayout;
7 import java.awt.event.ActionEvent;
8 import java.awt.event.ActionListener;
9 import java.awt.event.MouseAdapter;
10 import java.awt.event.MouseEvent;
11 import java.io.BufferedReader;
12 import java.io.IOException;
13 import java.io.InputStreamReader;
14 import java.io.PrintWriter;
15 import java.io.StringWriter;
16 import java.net.URL;
17 import java.util.HashMap;
18 import java.util.Map;
19
20 import javax.swing.Box;
21 import javax.swing.ButtonGroup;
22 import javax.swing.JButton;
23 import javax.swing.JComboBox;
24 import javax.swing.JOptionPane;
25 import javax.swing.JPanel;
26 import javax.swing.JProgressBar;
27 import javax.swing.JRadioButton;
28 import javax.swing.JScrollPane;
29 import javax.swing.JTable;
30 import javax.swing.JTextArea;
31 import javax.swing.border.EmptyBorder;
32 import javax.swing.table.DefaultTableCellRenderer;
33 import javax.swing.table.DefaultTableModel;
34
35 import jp.sourceforge.stigmata.Main;
36 import jp.sourceforge.stigmata.utils.HermesUtility;
37 import jp.sourceforge.talisman.hermes.HermesEvent;
38 import jp.sourceforge.talisman.hermes.HermesException;
39 import jp.sourceforge.talisman.hermes.HermesPercentageListener;
40 import jp.sourceforge.talisman.hermes.InvalidHermesConfigException;
41 import jp.sourceforge.talisman.hermes.maven.Artifact;
42 import jp.sourceforge.talisman.hermes.maven.License;
43 import jp.sourceforge.talisman.i18n.Messages;
44
45 import org.apache.commons.cli.ParseException;
46
47 public class UpdatePluginsPane extends JPanel{
48     private static final long serialVersionUID = 7595296740059360819L;
49
50     private StigmataFrame stigmata;
51     private HermesUtility hermes;
52     private DefaultTableModel model;
53     private JTable table;
54
55     public UpdatePluginsPane(StigmataFrame stigmata){
56         this.stigmata = stigmata;
57         hermes = new HermesUtility();
58
59         initLayout();
60         reload();
61     }
62
63     public void reload(){
64         try{
65             hermes.loadHermesContext(stigmata.getEnvironment());
66         } catch(InvalidHermesConfigException e){
67             GUIUtility.showErrorDialog(stigmata, stigmata.getMessages(), e);
68         } catch(IOException e){
69             GUIUtility.showErrorDialog(stigmata, stigmata.getMessages(), e);
70         }
71     }
72
73     private void updateArtifacts() throws IOException, HermesException{
74         UpdatePluginsPaneHermesListener listener = new UpdatePluginsPaneHermesListener(this, model);
75         hermes.getHermes().addHermesListener(listener);
76         hermes.update();
77         hermes.getHermes().removeHermesListener(listener);
78         hermes.updateContext(stigmata.getEnvironment());
79         Messages m = stigmata.getMessages();
80         int value = JOptionPane.showOptionDialog(
81             stigmata, m.get("restart.stigmata.requrested"),
82             m.get("message.dialog.title"),
83             JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null,
84             new String[] { m.get("restart.now"), m.get("restart.later"), }, null
85         );
86         if(value == JOptionPane.YES_OPTION){
87             stigmata.dispose();
88             try{
89                 new Main(new String[0]);
90             } catch(ParseException e){
91             }
92         }
93     }
94
95     private boolean applyLicenses() throws IOException, HermesException{
96         LicensePane licensePane = new LicensePane(stigmata, hermes.getUpdateTarget());
97         JOptionPane.showMessageDialog(stigmata, licensePane);
98         return licensePane.isApply();
99     }
100
101     private void showLists() throws IOException, HermesException{
102         Artifact[] artifacts = hermes.getUpdateTarget();
103         showLists(artifacts);
104     }
105
106     private void showLists(Artifact[] artifacts){
107         model.setRowCount(0);
108         for(Artifact artifact: artifacts){
109             Artifact original = hermes.getCurrentArtifact(artifact.getGroupId(), artifact.getArtifactId());
110             model.addRow(new Object[] { 
111                 artifact.getGroupId(), artifact.getArtifactId(),
112                 original.getVersion(), artifact.getVersion(), 
113                 artifact.getScope(), ProgressRenderer.NOT_STARTED
114             });
115         }
116         if(artifacts.length == 0){
117             JOptionPane.showMessageDialog(stigmata, stigmata.getMessages().get("availabe.artifacts.notfound"));
118         }
119     }
120
121     private void initLayout(){
122         Box buttonPane = Box.createHorizontalBox();
123
124         JButton checkButton = GUIUtility.createButton(stigmata.getMessages(), "hermes.check");
125         final JButton updateButton = GUIUtility.createButton(stigmata.getMessages(), "hermes.update");
126
127         checkButton.addActionListener(new ActionListener(){
128             @Override
129             public void actionPerformed(ActionEvent e){
130                 try{
131                     showLists();
132                     updateButton.setEnabled(model.getRowCount() > 0);
133                 } catch(IOException e1){
134                     GUIUtility.showErrorDialog(stigmata, stigmata.getMessages(), e1);
135                 } catch(HermesException e1){
136                     GUIUtility.showErrorDialog(stigmata, stigmata.getMessages(), e1);
137                 }
138             }
139         });
140         updateButton.addActionListener(new ActionListener(){
141             @Override
142             public void actionPerformed(ActionEvent e){
143                 try{
144                     if(applyLicenses()){
145                         updateArtifacts();
146                         updateButton.setEnabled(false);
147                     }
148                 } catch(IOException e1){
149                     GUIUtility.showErrorDialog(stigmata, stigmata.getMessages(), e1);
150                 } catch(HermesException e1){
151                     GUIUtility.showErrorDialog(stigmata, stigmata.getMessages(), e1);
152                 }
153             }
154         });
155         updateButton.setEnabled(false);
156
157         buttonPane.add(Box.createHorizontalGlue());
158         buttonPane.add(checkButton);
159         buttonPane.add(Box.createHorizontalGlue());
160         buttonPane.add(updateButton);
161         buttonPane.add(Box.createHorizontalGlue());
162
163         UneditableDefaultTableModel uneditableModel = new UneditableDefaultTableModel();
164         uneditableModel.setColumnClass(ProgressRenderer.PROGRESS_COLUMN, Integer.class);
165         uneditableModel.setColumnIdentifiers(stigmata.getMessages().getArray("hermes.artifacts.labels"));
166
167         table = new JTable(uneditableModel);
168         table.setDefaultRenderer(Integer.class, new ProgressRenderer());
169
170         setLayout(new BorderLayout());
171         add(new JScrollPane(table, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS), BorderLayout.CENTER);
172         add(buttonPane, BorderLayout.SOUTH);
173
174         this.model = uneditableModel;
175     }
176
177     private static class UpdatePluginsPaneHermesListener implements HermesPercentageListener{
178         private DefaultTableModel model;
179         private UpdatePluginsPane pane;
180         private Map<String, Map<String, Integer>> map;
181
182         public UpdatePluginsPaneHermesListener(UpdatePluginsPane pane, DefaultTableModel model){
183             this.model = model;
184             this.pane = pane;
185         }
186
187         @Override
188         public void downloadDone(HermesEvent e){
189             Artifact a = e.getArtifact();
190             model.setValueAt(ProgressRenderer.DONE, map.get(a.getGroupId()).get(a.getArtifactId()), ProgressRenderer.PROGRESS_COLUMN);
191         }
192
193         @Override
194         public void fileSizeGotten(HermesEvent e){
195         }
196
197         @Override
198         public void progress(HermesEvent e, double progress){
199             Artifact a = e.getArtifact();
200             model.setValueAt(
201                 (int)(progress * 100), map.get(a.getGroupId()).get(a.getArtifactId()),
202                 ProgressRenderer.PROGRESS_COLUMN
203             );
204         }
205
206         @Override
207         public void finish(HermesEvent e){
208         }
209
210         @Override
211         public void targetResolved(HermesEvent e){
212             Artifact[] artifacts = e.getArtifacts();
213             map = buildArtifactsIndexMap(artifacts);
214             pane.showLists(artifacts);
215         }
216
217         private Map<String, Map<String, Integer>> buildArtifactsIndexMap(Artifact[] artifacts){
218             Map<String, Map<String, Integer>> map = new HashMap<String, Map<String, Integer>>();
219         
220             for(int i = 0; i < artifacts.length; i++){
221                 String groupId = artifacts[i].getGroupId();
222                 Map<String, Integer> submap = map.get(groupId);
223                 if(submap == null){
224                     submap = new HashMap<String, Integer>();
225                     map.put(groupId, submap);
226                 }
227                 submap.put(artifacts[i].getArtifactId(), i);
228             }
229         
230             return map;
231         }
232     }
233
234
235     private static class ProgressRenderer extends DefaultTableCellRenderer{
236         private static final long serialVersionUID = 3098530332351108648L;
237
238         private static final int PROGRESS_COLUMN = 5;
239         public static final int DONE = 100;
240         public static final int NOT_STARTED = -1;
241         public static final int CANCELED = -2;
242
243         private JProgressBar progressBar = new JProgressBar(0, 100);
244
245         public ProgressRenderer(){
246             super();
247             setOpaque(true);
248             progressBar.setBorder(new EmptyBorder(1, 1, 1, 1));
249         }
250
251         @Override
252         public Component getTableCellRendererComponent(JTable table, Object originalValue, boolean isSelected, boolean hasFocus, int row, int column){
253             int value = ((Integer)originalValue).intValue();
254             progressBar.setValue(value);
255
256             return progressBar;
257         }
258     }
259
260     private static class LicensePane extends JPanel{
261         private static final long serialVersionUID = -1992258036940405393L;
262
263         private StigmataFrame parent;
264         private Map<License, String> map = new HashMap<License, String>();
265         private Artifact[] artifacts;
266         private DefaultTableModel model = new UneditableDefaultTableModel();
267         private JTextArea area;
268         private JRadioButton applyButton;
269         private JRadioButton discardButton;
270         
271
272         public LicensePane(StigmataFrame parent, Artifact[] artifacts){
273             this.parent = parent;
274             this.artifacts = artifacts;
275
276             initLayout();
277         }
278
279         public boolean isApply(){
280             return applyButton.isSelected();
281         }
282
283         private void showLicense(License license){
284             String licenseTerm = map.get(license);
285             if(licenseTerm == null){
286                 try{
287                     licenseTerm = loadLicenseTerm(license);
288                 } catch(IOException e){
289                     GUIUtility.showErrorDialog(parent, parent.getMessages(), e);
290                 }
291             }
292             area.setText(licenseTerm);
293         }
294
295         private String loadLicenseTerm(License license) throws IOException{
296             URL url = new URL(license.getUrl());
297             StringWriter out = new StringWriter();
298             PrintWriter writer = new PrintWriter(out);
299
300             BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
301             String line;
302             while((line = in.readLine()) != null){
303                 writer.println(line);
304             }
305             in.close();
306             writer.close();
307
308             String term = out.toString();
309
310             map.put(license, term);
311             return term;
312         }
313
314         private void initLayout(){
315             Messages messages = parent.getMessages();
316             JTable table = new JTable(model);
317             final JComboBox licenseNames = new JComboBox();
318
319             model.setColumnIdentifiers(messages.getArray("hermes.artifacts.basic.labels"));
320             area = new JTextArea();
321
322             applyButton = new JRadioButton(messages.get("apply.licenses"));
323             discardButton = new JRadioButton(messages.get("discard.licenses"), true);
324             ButtonGroup group = new ButtonGroup();
325             group.add(applyButton);
326             group.add(discardButton);
327
328             for(Artifact artifact: artifacts){
329                 model.addRow(new String[] { artifact.getGroupId(), artifact.getArtifactId(), artifact.getVersion(), });
330             }
331             table.addMouseListener(new MouseAdapter(){
332                 @Override
333                 public void mouseClicked(MouseEvent e){
334                     int row = ((JTable)e.getSource()).rowAtPoint(e.getPoint());
335                     License[] licenses = artifacts[row].getPom().getLicenses();
336
337                     licenseNames.removeAllItems();
338                     for(int i = 0; i < licenses.length; i++){
339                         licenseNames.addItem(licenses[i].getName());
340                     }
341                     if(licenses.length > 0){
342                         licenseNames.setSelectedIndex(0);
343                     }
344                     else{
345                         licenseNames.addItem(parent.getMessages().get("no.valid.licenses"));
346                     }
347                 }
348             });
349             licenseNames.addActionListener(new ActionListener(){
350                 @Override
351                 public void actionPerformed(ActionEvent e){
352                     String name = (String)((JComboBox)e.getSource()).getSelectedItem();
353                     boolean missingLicenseFlag = true;
354                     for(Map.Entry<License, String> entry: map.entrySet()){
355                         License license = entry.getKey();
356                         if(license.getName().equals(name)){
357                             missingLicenseFlag = false;
358                             showLicense(license);
359                         }
360                     }
361                     if(missingLicenseFlag){
362                         area.setText(parent.getMessages().get("no.valid.licenses"));
363                     }
364                 }
365             });
366
367             setLayout(new GridBagLayout());
368             GridBagConstraints gbc = new GridBagConstraints();
369             gbc.gridx = 0;
370             gbc.gridy = 0;
371             gbc.gridheight = 2;
372             gbc.gridwidth = 1;
373             gbc.weightx = 0.5d;
374             gbc.weighty = 1d;
375             gbc.fill = GridBagConstraints.BOTH;
376             add(new JScrollPane(table), gbc);
377             gbc.gridheight = 1;
378             gbc.gridx = 1;
379             gbc.weightx = 0.5d;
380             gbc.weighty = 0d;
381             gbc.fill = GridBagConstraints.HORIZONTAL;
382             add(licenseNames, gbc);
383             gbc.gridy = 1;
384             gbc.fill = GridBagConstraints.BOTH;
385             add(new JScrollPane(area), gbc);
386             gbc.gridwidth = 2;
387             gbc.gridx = 0;
388             gbc.gridy = 2;
389             gbc.weightx = 1d;
390             gbc.fill = GridBagConstraints.HORIZONTAL;
391             gbc.anchor = GridBagConstraints.WEST;
392             add(applyButton, gbc);
393             gbc.gridy = 3;
394             add(discardButton, gbc);
395         }
396     }
397 }