1 package jp.sourceforge.stigmata.ui.swing;
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;
17 import java.util.HashMap;
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;
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;
45 import org.apache.commons.cli.ParseException;
47 public class UpdatePluginsPane extends JPanel{
48 private static final long serialVersionUID = 7595296740059360819L;
50 private StigmataFrame stigmata;
51 private HermesUtility hermes;
52 private DefaultTableModel model;
55 public UpdatePluginsPane(StigmataFrame stigmata){
56 this.stigmata = stigmata;
57 hermes = new HermesUtility();
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);
73 private void updateArtifacts() throws IOException, HermesException{
74 UpdatePluginsPaneHermesListener listener = new UpdatePluginsPaneHermesListener(this, model);
75 hermes.getHermes().addHermesListener(listener);
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
86 if(value == JOptionPane.YES_OPTION){
89 new Main(new String[0]);
90 } catch(ParseException e){
95 private boolean applyLicenses() throws IOException, HermesException{
96 LicensePane licensePane = new LicensePane(stigmata, hermes.getUpdateTarget());
97 JOptionPane.showMessageDialog(stigmata, licensePane);
98 return licensePane.isApply();
101 private void showLists() throws IOException, HermesException{
102 Artifact[] artifacts = hermes.getUpdateTarget();
103 showLists(artifacts);
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
116 if(artifacts.length == 0){
117 JOptionPane.showMessageDialog(stigmata, stigmata.getMessages().get("availabe.artifacts.notfound"));
121 private void initLayout(){
122 Box buttonPane = Box.createHorizontalBox();
124 JButton checkButton = GUIUtility.createButton(stigmata.getMessages(), "hermes.check");
125 final JButton updateButton = GUIUtility.createButton(stigmata.getMessages(), "hermes.update");
127 checkButton.addActionListener(new ActionListener(){
129 public void actionPerformed(ActionEvent e){
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);
140 updateButton.addActionListener(new ActionListener(){
142 public void actionPerformed(ActionEvent e){
146 updateButton.setEnabled(false);
148 } catch(IOException e1){
149 GUIUtility.showErrorDialog(stigmata, stigmata.getMessages(), e1);
150 } catch(HermesException e1){
151 GUIUtility.showErrorDialog(stigmata, stigmata.getMessages(), e1);
155 updateButton.setEnabled(false);
157 buttonPane.add(Box.createHorizontalGlue());
158 buttonPane.add(checkButton);
159 buttonPane.add(Box.createHorizontalGlue());
160 buttonPane.add(updateButton);
161 buttonPane.add(Box.createHorizontalGlue());
163 UneditableDefaultTableModel uneditableModel = new UneditableDefaultTableModel();
164 uneditableModel.setColumnClass(ProgressRenderer.PROGRESS_COLUMN, Integer.class);
165 uneditableModel.setColumnIdentifiers(stigmata.getMessages().getArray("hermes.artifacts.labels"));
167 table = new JTable(uneditableModel);
168 table.setDefaultRenderer(Integer.class, new ProgressRenderer());
170 setLayout(new BorderLayout());
171 add(new JScrollPane(table, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS), BorderLayout.CENTER);
172 add(buttonPane, BorderLayout.SOUTH);
174 this.model = uneditableModel;
177 private static class UpdatePluginsPaneHermesListener implements HermesPercentageListener{
178 private DefaultTableModel model;
179 private UpdatePluginsPane pane;
180 private Map<String, Map<String, Integer>> map;
182 public UpdatePluginsPaneHermesListener(UpdatePluginsPane pane, DefaultTableModel model){
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);
194 public void fileSizeGotten(HermesEvent e){
198 public void progress(HermesEvent e, double progress){
199 Artifact a = e.getArtifact();
201 (int)(progress * 100), map.get(a.getGroupId()).get(a.getArtifactId()),
202 ProgressRenderer.PROGRESS_COLUMN
207 public void finish(HermesEvent e){
211 public void targetResolved(HermesEvent e){
212 Artifact[] artifacts = e.getArtifacts();
213 map = buildArtifactsIndexMap(artifacts);
214 pane.showLists(artifacts);
217 private Map<String, Map<String, Integer>> buildArtifactsIndexMap(Artifact[] artifacts){
218 Map<String, Map<String, Integer>> map = new HashMap<String, Map<String, Integer>>();
220 for(int i = 0; i < artifacts.length; i++){
221 String groupId = artifacts[i].getGroupId();
222 Map<String, Integer> submap = map.get(groupId);
224 submap = new HashMap<String, Integer>();
225 map.put(groupId, submap);
227 submap.put(artifacts[i].getArtifactId(), i);
235 private static class ProgressRenderer extends DefaultTableCellRenderer{
236 private static final long serialVersionUID = 3098530332351108648L;
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;
243 private JProgressBar progressBar = new JProgressBar(0, 100);
245 public ProgressRenderer(){
248 progressBar.setBorder(new EmptyBorder(1, 1, 1, 1));
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);
260 private static class LicensePane extends JPanel{
261 private static final long serialVersionUID = -1992258036940405393L;
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;
272 public LicensePane(StigmataFrame parent, Artifact[] artifacts){
273 this.parent = parent;
274 this.artifacts = artifacts;
279 public boolean isApply(){
280 return applyButton.isSelected();
283 private void showLicense(License license){
284 String licenseTerm = map.get(license);
285 if(licenseTerm == null){
287 licenseTerm = loadLicenseTerm(license);
288 } catch(IOException e){
289 GUIUtility.showErrorDialog(parent, parent.getMessages(), e);
292 area.setText(licenseTerm);
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);
300 BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
302 while((line = in.readLine()) != null){
303 writer.println(line);
308 String term = out.toString();
310 map.put(license, term);
314 private void initLayout(){
315 Messages messages = parent.getMessages();
316 JTable table = new JTable(model);
317 final JComboBox licenseNames = new JComboBox();
319 model.setColumnIdentifiers(messages.getArray("hermes.artifacts.basic.labels"));
320 area = new JTextArea();
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);
328 for(Artifact artifact: artifacts){
329 model.addRow(new String[] { artifact.getGroupId(), artifact.getArtifactId(), artifact.getVersion(), });
331 table.addMouseListener(new MouseAdapter(){
333 public void mouseClicked(MouseEvent e){
334 int row = ((JTable)e.getSource()).rowAtPoint(e.getPoint());
335 License[] licenses = artifacts[row].getPom().getLicenses();
337 licenseNames.removeAllItems();
338 for(int i = 0; i < licenses.length; i++){
339 licenseNames.addItem(licenses[i].getName());
341 if(licenses.length > 0){
342 licenseNames.setSelectedIndex(0);
345 licenseNames.addItem(parent.getMessages().get("no.valid.licenses"));
349 licenseNames.addActionListener(new ActionListener(){
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);
361 if(missingLicenseFlag){
362 area.setText(parent.getMessages().get("no.valid.licenses"));
367 setLayout(new GridBagLayout());
368 GridBagConstraints gbc = new GridBagConstraints();
375 gbc.fill = GridBagConstraints.BOTH;
376 add(new JScrollPane(table), gbc);
381 gbc.fill = GridBagConstraints.HORIZONTAL;
382 add(licenseNames, gbc);
384 gbc.fill = GridBagConstraints.BOTH;
385 add(new JScrollPane(area), gbc);
390 gbc.fill = GridBagConstraints.HORIZONTAL;
391 gbc.anchor = GridBagConstraints.WEST;
392 add(applyButton, gbc);
394 add(discardButton, gbc);