OSDN Git Service

ea23aeab6fc791801f61832c97051cb7d1787f3f
[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             public void actionPerformed(ActionEvent e){
129                 try{
130                     showLists();
131                     updateButton.setEnabled(model.getRowCount() > 0);
132                 } catch(IOException e1){
133                     GUIUtility.showErrorDialog(stigmata, stigmata.getMessages(), e1);
134                 } catch(HermesException e1){
135                     GUIUtility.showErrorDialog(stigmata, stigmata.getMessages(), e1);
136                 }
137             }
138         });
139         updateButton.addActionListener(new ActionListener(){
140             public void actionPerformed(ActionEvent e){
141                 try{
142                     if(applyLicenses()){
143                         updateArtifacts();
144                         updateButton.setEnabled(false);
145                     }
146                 } catch(IOException e1){
147                     GUIUtility.showErrorDialog(stigmata, stigmata.getMessages(), e1);
148                 } catch(HermesException e1){
149                     GUIUtility.showErrorDialog(stigmata, stigmata.getMessages(), e1);
150                 }
151             }
152         });
153         updateButton.setEnabled(false);
154
155         buttonPane.add(Box.createHorizontalGlue());
156         buttonPane.add(checkButton);
157         buttonPane.add(Box.createHorizontalGlue());
158         buttonPane.add(updateButton);
159         buttonPane.add(Box.createHorizontalGlue());
160
161         UneditableDefaultTableModel uneditableModel = new UneditableDefaultTableModel();
162         uneditableModel.setColumnClass(ProgressRenderer.PROGRESS_COLUMN, Integer.class);
163         uneditableModel.setColumnIdentifiers(stigmata.getMessages().getArray("hermes.artifacts.labels"));
164
165         table = new JTable(uneditableModel);
166         table.setDefaultRenderer(Integer.class, new ProgressRenderer());
167
168         setLayout(new BorderLayout());
169         add(new JScrollPane(table, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS), BorderLayout.CENTER);
170         add(buttonPane, BorderLayout.SOUTH);
171
172         this.model = uneditableModel;
173     }
174
175     private static class UpdatePluginsPaneHermesListener implements HermesPercentageListener{
176         private DefaultTableModel model;
177         private UpdatePluginsPane pane;
178         private Map<String, Map<String, Integer>> map;
179
180         public UpdatePluginsPaneHermesListener(UpdatePluginsPane pane, DefaultTableModel model){
181             this.model = model;
182             this.pane = pane;
183         }
184
185         public void downloadDone(HermesEvent e){
186             Artifact a = e.getArtifact();
187             model.setValueAt(ProgressRenderer.DONE, map.get(a.getGroupId()).get(a.getArtifactId()), ProgressRenderer.PROGRESS_COLUMN);
188         }
189
190         public void fileSizeGotten(HermesEvent e){
191         }
192
193         public void progress(HermesEvent e, double progress){
194             Artifact a = e.getArtifact();
195             model.setValueAt(
196                 (int)(progress * 100), map.get(a.getGroupId()).get(a.getArtifactId()),
197                 ProgressRenderer.PROGRESS_COLUMN
198             );
199         }
200
201         public void finish(HermesEvent e){
202         }
203
204         public void targetResolved(HermesEvent e){
205             Artifact[] artifacts = e.getArtifacts();
206             map = buildArtifactsIndexMap(artifacts);
207             pane.showLists(artifacts);
208         }
209
210         private Map<String, Map<String, Integer>> buildArtifactsIndexMap(Artifact[] artifacts){
211             Map<String, Map<String, Integer>> map = new HashMap<String, Map<String, Integer>>();
212         
213             for(int i = 0; i < artifacts.length; i++){
214                 String groupId = artifacts[i].getGroupId();
215                 Map<String, Integer> submap = map.get(groupId);
216                 if(submap == null){
217                     submap = new HashMap<String, Integer>();
218                     map.put(groupId, submap);
219                 }
220                 submap.put(artifacts[i].getArtifactId(), i);
221             }
222         
223             return map;
224         }
225     }
226
227
228     private static class ProgressRenderer extends DefaultTableCellRenderer{
229         private static final long serialVersionUID = 3098530332351108648L;
230
231         private static final int PROGRESS_COLUMN = 5;
232         public static final int DONE = 100;
233         public static final int NOT_STARTED = -1;
234         public static final int CANCELED = -2;
235
236         private JProgressBar progressBar = new JProgressBar(0, 100);
237
238         public ProgressRenderer(){
239             super();
240             setOpaque(true);
241             progressBar.setBorder(new EmptyBorder(1, 1, 1, 1));
242         }
243
244         @Override
245         public Component getTableCellRendererComponent(JTable table, Object originalValue, boolean isSelected, boolean hasFocus, int row, int column){
246             int value = ((Integer)originalValue).intValue();
247             progressBar.setValue(value);
248
249             return progressBar;
250         }
251     }
252
253     private static class LicensePane extends JPanel{
254         private static final long serialVersionUID = -1992258036940405393L;
255
256         private StigmataFrame parent;
257         private Map<License, String> map = new HashMap<License, String>();
258         private Artifact[] artifacts;
259         private DefaultTableModel model = new UneditableDefaultTableModel();
260         private JTextArea area;
261         private JRadioButton applyButton;
262         private JRadioButton discardButton;
263         
264
265         public LicensePane(StigmataFrame parent, Artifact[] artifacts){
266             this.parent = parent;
267             this.artifacts = artifacts;
268
269             initLayout();
270         }
271
272         public boolean isApply(){
273             return applyButton.isSelected();
274         }
275
276         private void showLicense(License license){
277             String licenseTerm = map.get(license);
278             if(licenseTerm == null){
279                 try{
280                     licenseTerm = loadLicenseTerm(license);
281                 } catch(IOException e){
282                     GUIUtility.showErrorDialog(parent, parent.getMessages(), e);
283                 }
284             }
285             area.setText(licenseTerm);
286         }
287
288         private String loadLicenseTerm(License license) throws IOException{
289             URL url = new URL(license.getUrl());
290             StringWriter out = new StringWriter();
291             PrintWriter writer = new PrintWriter(out);
292
293             BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
294             String line;
295             while((line = in.readLine()) != null){
296                 writer.println(line);
297             }
298             in.close();
299             writer.close();
300
301             String term = out.toString();
302
303             map.put(license, term);
304             return term;
305         }
306
307         private void initLayout(){
308             Messages messages = parent.getMessages();
309             JTable table = new JTable(model);
310             final JComboBox licenseNames = new JComboBox();
311
312             model.setColumnIdentifiers(messages.getArray("hermes.artifacts.basic.labels"));
313             area = new JTextArea();
314
315             applyButton = new JRadioButton(messages.get("apply.licenses"));
316             discardButton = new JRadioButton(messages.get("discard.licenses"), true);
317             ButtonGroup group = new ButtonGroup();
318             group.add(applyButton);
319             group.add(discardButton);
320
321             for(Artifact artifact: artifacts){
322                 model.addRow(new String[] { artifact.getGroupId(), artifact.getArtifactId(), artifact.getVersion(), });
323             }
324             table.addMouseListener(new MouseAdapter(){
325                 public void mouseClicked(MouseEvent e){
326                     int row = ((JTable)e.getSource()).rowAtPoint(e.getPoint());
327                     License[] licenses = artifacts[row].getPom().getLicenses();
328
329                     licenseNames.removeAllItems();
330                     for(int i = 0; i < licenses.length; i++){
331                         licenseNames.addItem(licenses[i]);
332                     }
333                     if(licenses.length > 0){
334                         licenseNames.setSelectedIndex(0);
335                     }
336                     else{
337                         licenseNames.addItem(parent.getMessages().get("no.valid.licenses"));
338                     }
339                 }
340             });
341             licenseNames.addActionListener(new ActionListener(){
342                 public void actionPerformed(ActionEvent e){
343                     String name = (String)((JComboBox)e.getSource()).getSelectedItem();
344                     boolean missingLicenseFlag = true;
345                     for(Map.Entry<License, String> entry: map.entrySet()){
346                         License license = entry.getKey();
347                         if(license.getName().equals(name)){
348                             missingLicenseFlag = false;
349                             showLicense(license);
350                         }
351                     }
352                     if(missingLicenseFlag){
353                         area.setText(parent.getMessages().get("no.valid.licenses"));
354                     }
355                 }
356             });
357
358             setLayout(new GridBagLayout());
359             GridBagConstraints gbc = new GridBagConstraints();
360             gbc.gridx = 0;
361             gbc.gridy = 0;
362             gbc.gridheight = 2;
363             gbc.gridwidth = 1;
364             gbc.weightx = 0.5d;
365             gbc.weighty = 1d;
366             gbc.fill = GridBagConstraints.BOTH;
367             add(new JScrollPane(table), gbc);
368             gbc.gridheight = 1;
369             gbc.gridx = 1;
370             gbc.weightx = 0.5d;
371             gbc.weighty = 0d;
372             gbc.fill = GridBagConstraints.HORIZONTAL;
373             add(licenseNames, gbc);
374             gbc.gridy = 1;
375             gbc.fill = GridBagConstraints.BOTH;
376             add(new JScrollPane(area), gbc);
377             gbc.gridwidth = 2;
378             gbc.gridx = 0;
379             gbc.gridy = 2;
380             gbc.weightx = 1d;
381             gbc.fill = GridBagConstraints.HORIZONTAL;
382             gbc.anchor = GridBagConstraints.WEST;
383             add(applyButton, gbc);
384             gbc.gridy = 3;
385             add(discardButton, gbc);
386         }
387     }
388 }