This project has retired. For details please refer to its Attic page.
InfoPanel xref

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   * http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.chemistry.opencmis.workbench.swing;
20  
21  import java.awt.Color;
22  import java.awt.Cursor;
23  import java.awt.Desktop;
24  import java.awt.FlowLayout;
25  import java.awt.Font;
26  import java.awt.FontMetrics;
27  import java.awt.Graphics;
28  import java.awt.GridBagConstraints;
29  import java.awt.GridBagLayout;
30  import java.awt.Insets;
31  import java.awt.Rectangle;
32  import java.awt.Toolkit;
33  import java.awt.datatransfer.Clipboard;
34  import java.awt.datatransfer.StringSelection;
35  import java.awt.datatransfer.Transferable;
36  import java.awt.event.ActionEvent;
37  import java.awt.event.ActionListener;
38  import java.awt.event.MouseEvent;
39  import java.awt.event.MouseListener;
40  import java.net.URI;
41  import java.util.Collection;
42  
43  import javax.swing.BorderFactory;
44  import javax.swing.BoxLayout;
45  import javax.swing.JComponent;
46  import javax.swing.JLabel;
47  import javax.swing.JMenuItem;
48  import javax.swing.JPanel;
49  import javax.swing.JPopupMenu;
50  import javax.swing.JTextField;
51  import javax.swing.SwingUtilities;
52  import javax.swing.UIManager;
53  
54  import org.apache.chemistry.opencmis.workbench.ClientHelper;
55  import org.apache.chemistry.opencmis.workbench.model.ClientModel;
56  
57  public abstract class InfoPanel extends JPanel {
58  
59      private static final long serialVersionUID = 1L;
60  
61      private final ClientModel model;
62  
63      private JPanel gridPanel;
64      private GridBagConstraints gbc;
65      private Font boldFont;
66  
67      public InfoPanel(ClientModel model) {
68          this.model = model;
69      }
70  
71      protected ClientModel getClientModel() {
72          return model;
73      }
74  
75      protected void setupGUI() {
76          setLayout(new FlowLayout(FlowLayout.LEFT));
77          setBackground(Color.WHITE);
78  
79          gridPanel = new JPanel(new GridBagLayout());
80          gridPanel.setBackground(Color.WHITE);
81          add(gridPanel);
82  
83          gbc = new GridBagConstraints();
84  
85          gbc.fill = GridBagConstraints.BOTH;
86          gbc.gridy = 0;
87          gbc.insets = new Insets(3, 3, 3, 3);
88  
89          Font labelFont = UIManager.getFont("Label.font");
90          boldFont = labelFont.deriveFont(Font.BOLD, labelFont.getSize2D() * 1.2f);
91      }
92  
93      protected JTextField addLine(final String label) {
94          return addLine(label, false);
95      }
96  
97      protected JTextField addLine(final String label, final boolean bold) {
98          return addLine(label, bold, new JTextField());
99      }
100 
101     protected JTextField addLine(final String label, final boolean bold, JTextField textField) {
102         textField.setEditable(false);
103         textField.setBorder(BorderFactory.createEmptyBorder());
104         if (bold) {
105             textField.setFont(boldFont);
106         }
107 
108         JLabel textLable = new JLabel(label);
109         textLable.setLabelFor(textField);
110         if (bold) {
111             textLable.setFont(boldFont);
112         }
113 
114         gbc.gridy++;
115 
116         gbc.gridx = 0;
117         gbc.anchor = GridBagConstraints.BASELINE_TRAILING;
118         gridPanel.add(textLable, gbc);
119 
120         gbc.gridx = 1;
121         gbc.anchor = GridBagConstraints.BASELINE_LEADING;
122         gridPanel.add(textField, gbc);
123 
124         return textField;
125     }
126 
127     protected JTextField addId(final String label) {
128         return addLine(label, false, new IdTextField());
129     }
130 
131     protected JTextField addLink(final String label) {
132         return addLine(label, false, new UrlTextField());
133     }
134 
135     protected YesNoLabel addYesNoLabel(String label) {
136         YesNoLabel ynl = new YesNoLabel();
137 
138         JLabel textLable = new JLabel(label);
139         textLable.setLabelFor(ynl);
140 
141         gbc.gridy++;
142 
143         gbc.gridx = 0;
144         gbc.anchor = GridBagConstraints.BASELINE_TRAILING;
145         gridPanel.add(textLable, gbc);
146 
147         gbc.gridx = 1;
148         gbc.anchor = GridBagConstraints.BASELINE_LEADING;
149         gridPanel.add(ynl, gbc);
150 
151         return ynl;
152     }
153 
154     protected <T extends JComponent> T addComponent(String label, T comp) {
155         JLabel textLable = new JLabel(label);
156 
157         JPanel panel = new JPanel();
158         panel.setLayout(new BoxLayout(panel, BoxLayout.LINE_AXIS));
159         panel.setBorder(BorderFactory.createEmptyBorder());
160         panel.setOpaque(false);
161         panel.add(comp);
162         textLable.setLabelFor(panel);
163 
164         gbc.gridy++;
165 
166         gbc.gridx = 0;
167         gbc.anchor = GridBagConstraints.BASELINE_TRAILING;
168         gridPanel.add(textLable, gbc);
169 
170         gbc.gridx = 1;
171         gbc.anchor = GridBagConstraints.BASELINE_LEADING;
172         gridPanel.add(panel, gbc);
173 
174         return comp;
175     }
176 
177     public static class InfoList extends JPanel {
178         private static final long serialVersionUID = 1L;
179 
180         public InfoList() {
181             setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
182             setBackground(Color.WHITE);
183         }
184 
185         public void clear() {
186             removeAll();
187         }
188 
189         public void setList(Collection<?> list) {
190             clear();
191 
192             if (list == null || list.size() == 0) {
193                 return;
194             }
195 
196             for (Object o : list) {
197                 JTextField textField = new JTextField(o == null ? "" : o.toString());
198                 textField.setEditable(false);
199                 textField.setBorder(BorderFactory.createEmptyBorder(0, 0, 2, 0));
200                 add(textField);
201             }
202         }
203     }
204 
205     private abstract class ClickableTextField extends JTextField {
206         private static final long serialVersionUID = 1L;
207 
208         private String link;
209         private final JPopupMenu popup;
210 
211         public ClickableTextField() {
212             popup = new JPopupMenu();
213             final JMenuItem menuItem = new JMenuItem("Copy to clipboard");
214             popup.add(menuItem);
215 
216             menuItem.addActionListener(new ActionListener() {
217                 public void actionPerformed(ActionEvent e) {
218                     Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
219                     Transferable transferable = new StringSelection(link);
220                     clipboard.setContents(transferable, null);
221                 }
222             });
223 
224             addMouseListener(new MouseListener() {
225 
226                 @Override
227                 public void mouseExited(MouseEvent e) {
228                 }
229 
230                 @Override
231                 public void mouseEntered(MouseEvent e) {
232                 }
233 
234                 @Override
235                 public void mouseClicked(MouseEvent e) {
236                     if (link != null) {
237                         if (SwingUtilities.isLeftMouseButton(e) && e.getClickCount() == 1) {
238                             try {
239                                 linkAction(link);
240                             } catch (Exception ex) {
241                                 ClientHelper.showError(InfoPanel.this, ex);
242                             }
243                         }
244                     }
245                 }
246 
247                 @Override
248                 public void mousePressed(MouseEvent e) {
249                     maybeShowPopup(e);
250                 }
251 
252                 @Override
253                 public void mouseReleased(MouseEvent e) {
254                     maybeShowPopup(e);
255                 }
256 
257                 private void maybeShowPopup(MouseEvent e) {
258                     if (e.isPopupTrigger()) {
259                         popup.show(e.getComponent(), e.getX(), e.getY());
260                     }
261                 }
262             });
263         }
264 
265         public abstract boolean isLink(String link);
266 
267         public abstract Color getLinkColor(String link);
268 
269         public abstract void linkAction(String link);
270 
271         @Override
272         public void setText(String text) {
273             if (!isLink(text)) {
274                 setForeground(UIManager.getColor("textForeground"));
275                 setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
276                 popup.setEnabled(false);
277                 link = null;
278             } else {
279                 setForeground(getLinkColor(text));
280                 setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
281                 popup.setEnabled(true);
282                 link = text;
283             }
284 
285             super.setText(text);
286         }
287 
288         @Override
289         public void validate() {
290         }
291 
292         @Override
293         public void revalidate() {
294         }
295 
296         @Override
297         public void repaint(long tm, int x, int y, int width, int height) {
298         }
299 
300         @Override
301         public void repaint(Rectangle r) {
302         }
303 
304         @Override
305         public void paintComponent(Graphics g) {
306             super.paintComponent(g);
307 
308             if (link != null) {
309                 FontMetrics fm = getFontMetrics(getFont());
310 
311                 int y1 = fm.getHeight() - 2;
312                 int x2 = fm.stringWidth(link);
313                 g.setColor(getLinkColor(link));
314                 g.drawLine(0, y1, x2, y1);
315             }
316         }
317     }
318 
319     private class IdTextField extends ClickableTextField {
320         private static final long serialVersionUID = 1L;
321 
322         public IdTextField() {
323             super();
324         }
325 
326         @Override
327         public boolean isLink(String link) {
328             return link != null && link.length() > 0 && !link.startsWith("(");
329         }
330 
331         @Override
332         public Color getLinkColor(String link) {
333             return ClientHelper.LINK_COLOR;
334         }
335 
336         @Override
337         public void linkAction(String link) {
338             try {
339                 getClientModel().loadObject(link);
340             } catch (Exception ex) {
341                 ClientHelper.showError(InfoPanel.this, ex);
342             }
343         }
344     }
345 
346     private class UrlTextField extends ClickableTextField {
347         private static final long serialVersionUID = 1L;
348 
349         public UrlTextField() {
350             super();
351         }
352 
353         @Override
354         public boolean isLink(String link) {
355             if (link == null || link.length() == 0) {
356                 return false;
357             }
358 
359             String lower = link.toLowerCase();
360             return lower.startsWith("http://") || lower.startsWith("https://");
361         }
362 
363         @Override
364         public Color getLinkColor(String link) {
365             return Color.BLUE;
366         }
367 
368         @Override
369         public void linkAction(String link) {
370             if (Desktop.isDesktopSupported()) {
371                 try {
372                     Desktop.getDesktop().browse(new URI(link));
373                 } catch (Exception ex) {
374                     ClientHelper.showError(InfoPanel.this, ex);
375                 }
376             }
377         }
378     }
379 }