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(){
128 public void actionPerformed(ActionEvent e){
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);
139 updateButton.addActionListener(new ActionListener(){
140 public void actionPerformed(ActionEvent e){
144 updateButton.setEnabled(false);
146 } catch(IOException e1){
147 GUIUtility.showErrorDialog(stigmata, stigmata.getMessages(), e1);
148 } catch(HermesException e1){
149 GUIUtility.showErrorDialog(stigmata, stigmata.getMessages(), e1);
153 updateButton.setEnabled(false);
155 buttonPane.add(Box.createHorizontalGlue());
156 buttonPane.add(checkButton);
157 buttonPane.add(Box.createHorizontalGlue());
158 buttonPane.add(updateButton);
159 buttonPane.add(Box.createHorizontalGlue());
161 UneditableDefaultTableModel uneditableModel = new UneditableDefaultTableModel();
162 uneditableModel.setColumnClass(ProgressRenderer.PROGRESS_COLUMN, Integer.class);
163 uneditableModel.setColumnIdentifiers(stigmata.getMessages().getArray("hermes.artifacts.labels"));
165 table = new JTable(uneditableModel);
166 table.setDefaultRenderer(Integer.class, new ProgressRenderer());
168 setLayout(new BorderLayout());
169 add(new JScrollPane(table, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS), BorderLayout.CENTER);
170 add(buttonPane, BorderLayout.SOUTH);
172 this.model = uneditableModel;
175 private static class UpdatePluginsPaneHermesListener implements HermesPercentageListener{
176 private DefaultTableModel model;
177 private UpdatePluginsPane pane;
178 private Map<String, Map<String, Integer>> map;
180 public UpdatePluginsPaneHermesListener(UpdatePluginsPane pane, DefaultTableModel model){
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);
190 public void fileSizeGotten(HermesEvent e){
193 public void progress(HermesEvent e, double progress){
194 Artifact a = e.getArtifact();
196 (int)(progress * 100), map.get(a.getGroupId()).get(a.getArtifactId()),
197 ProgressRenderer.PROGRESS_COLUMN
201 public void finish(HermesEvent e){
204 public void targetResolved(HermesEvent e){
205 Artifact[] artifacts = e.getArtifacts();
206 map = buildArtifactsIndexMap(artifacts);
207 pane.showLists(artifacts);
210 private Map<String, Map<String, Integer>> buildArtifactsIndexMap(Artifact[] artifacts){
211 Map<String, Map<String, Integer>> map = new HashMap<String, Map<String, Integer>>();
213 for(int i = 0; i < artifacts.length; i++){
214 String groupId = artifacts[i].getGroupId();
215 Map<String, Integer> submap = map.get(groupId);
217 submap = new HashMap<String, Integer>();
218 map.put(groupId, submap);
220 submap.put(artifacts[i].getArtifactId(), i);
228 private static class ProgressRenderer extends DefaultTableCellRenderer{
229 private static final long serialVersionUID = 3098530332351108648L;
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;
236 private JProgressBar progressBar = new JProgressBar(0, 100);
238 public ProgressRenderer(){
241 progressBar.setBorder(new EmptyBorder(1, 1, 1, 1));
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);
253 private static class LicensePane extends JPanel{
254 private static final long serialVersionUID = -1992258036940405393L;
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;
265 public LicensePane(StigmataFrame parent, Artifact[] artifacts){
266 this.parent = parent;
267 this.artifacts = artifacts;
272 public boolean isApply(){
273 return applyButton.isSelected();
276 private void showLicense(License license){
277 String licenseTerm = map.get(license);
278 if(licenseTerm == null){
280 licenseTerm = loadLicenseTerm(license);
281 } catch(IOException e){
282 GUIUtility.showErrorDialog(parent, parent.getMessages(), e);
285 area.setText(licenseTerm);
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);
293 BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
295 while((line = in.readLine()) != null){
296 writer.println(line);
301 String term = out.toString();
303 map.put(license, term);
307 private void initLayout(){
308 Messages messages = parent.getMessages();
309 JTable table = new JTable(model);
310 final JComboBox licenseNames = new JComboBox();
312 model.setColumnIdentifiers(messages.getArray("hermes.artifacts.basic.labels"));
313 area = new JTextArea();
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);
321 for(Artifact artifact: artifacts){
322 model.addRow(new String[] { artifact.getGroupId(), artifact.getArtifactId(), artifact.getVersion(), });
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();
329 licenseNames.removeAllItems();
330 for(int i = 0; i < licenses.length; i++){
331 licenseNames.addItem(licenses[i]);
333 if(licenses.length > 0){
334 licenseNames.setSelectedIndex(0);
337 licenseNames.addItem(parent.getMessages().get("no.valid.licenses"));
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);
352 if(missingLicenseFlag){
353 area.setText(parent.getMessages().get("no.valid.licenses"));
358 setLayout(new GridBagLayout());
359 GridBagConstraints gbc = new GridBagConstraints();
366 gbc.fill = GridBagConstraints.BOTH;
367 add(new JScrollPane(table), gbc);
372 gbc.fill = GridBagConstraints.HORIZONTAL;
373 add(licenseNames, gbc);
375 gbc.fill = GridBagConstraints.BOTH;
376 add(new JScrollPane(area), gbc);
381 gbc.fill = GridBagConstraints.HORIZONTAL;
382 gbc.anchor = GridBagConstraints.WEST;
383 add(applyButton, gbc);
385 add(discardButton, gbc);