1 package org.telscenter.pas.common.ui;
2
3 import java.awt.BorderLayout;
4 import java.awt.Color;
5 import java.awt.Component;
6 import java.awt.Container;
7 import java.awt.Cursor;
8 import java.awt.Dimension;
9 import java.awt.FileDialog;
10 import java.awt.Font;
11 import java.awt.Point;
12 import java.awt.Rectangle;
13 import java.awt.Toolkit;
14 import java.awt.event.ActionEvent;
15 import java.awt.event.ActionListener;
16 import java.awt.event.ComponentEvent;
17 import java.awt.event.ComponentListener;
18 import java.awt.event.MouseAdapter;
19 import java.awt.event.MouseEvent;
20 import java.awt.event.MouseListener;
21 import java.awt.event.MouseMotionListener;
22 import java.io.ByteArrayOutputStream;
23 import java.io.File;
24 import java.io.InputStream;
25 import java.io.PrintWriter;
26 import java.io.StringWriter;
27 import java.util.ArrayList;
28 import java.util.List;
29 import java.util.logging.Logger;
30
31 import javax.swing.BorderFactory;
32 import javax.swing.ImageIcon;
33 import javax.swing.JButton;
34 import javax.swing.JComponent;
35 import javax.swing.JDialog;
36 import javax.swing.JFrame;
37 import javax.swing.JLabel;
38 import javax.swing.JPanel;
39 import javax.swing.JScrollPane;
40 import javax.swing.JTextArea;
41 import javax.swing.ScrollPaneConstants;
42 import javax.swing.SwingUtilities;
43 import javax.swing.border.Border;
44 import javax.swing.border.EtchedBorder;
45 import javax.swing.text.JTextComponent;
46
47 import org.apache.commons.io.IOUtils;
48 import org.concord.swing.CCFileDialog;
49 import org.concord.swing.CCFilenameFilter;
50 import org.telscenter.pas.hint.HintUI;
51 import org.telscenter.pas.properties.HintSet;
52 import org.telscenter.pas.ui.border.InvertedBevelBorder;
53 import org.telscenter.pas.ui.dialog.PasDialogManager;
54 import org.telscenter.pas.ui.icons.PasCommonIconProvider;
55 import org.telscenter.pas.ui.util.PasColors;
56 import org.w3c.tidy.Tidy;
57 import org.xhtmlrenderer.simple.XHTMLPanel;
58
59 public class CommonUI {
60 /***
61 * Logger for this class
62 */
63 private static final Logger logger = Logger.getLogger(CommonUI.class
64 .getName());
65
66 public static final String MODE = "MODE";
67
68 public static final String SAVE_MODE = "SAVE";
69
70 public static final long serialVersionUID = 1L;
71
72 public static final String EDIT_MODE = "EDIT";
73
74 public static final String CREATE_Label = "Create";
75
76 public static final String EDIT_LABEL = "Edit";
77
78 public static final String EDIT_BUTTON = "EDIT_BUTTON";
79
80 public static final String TEXT_COMP = "TEXT_COMP";
81
82 public static final String EDIT_PANEL = "EDIT_PANEL";
83
84 public static final String DEFAULT_FILENAME = "authoring_curnit.otml";
85
86 public static String fileName = "";
87
88 public static String tidyHtml(String html) {
89 return tidyHtml(html, null);
90 }
91
92 /***
93 * @param formPanel
94 * @param textI
95 * @return
96 * @return
97 */
98 public static JComponent createHTMLTextPanel(String text) {
99
100 if (text == null)
101 return new JLabel("There was an error creating this html text.");
102
103 final XHTMLPanel panel = new XHTMLPanel();
104
105
106 panel.setOpaque(false);
107 panel.addComponentListener(new ComponentListener() {
108
109 public void componentHidden(ComponentEvent e) {
110
111
112 }
113
114 public void componentMoved(ComponentEvent e) {
115
116
117 }
118
119 public void componentResized(ComponentEvent e) {
120
121
122 }
123
124 public void componentShown(ComponentEvent e) {
125
126
127 }
128 });
129
130 InputStream stream = null;
131 try {
132 stream = IOUtils.toInputStream(text);
133 panel.setDocument(stream, null);
134 } catch (NullPointerException e) {
135 e.printStackTrace();
136 } catch (Exception e) {
137 e.printStackTrace();
138 } finally {
139 IOUtils.closeQuietly(stream);
140 }
141
142 return panel;
143
144 }
145
146 public static ActionListener createHTMLTidyEditActionlistener() {
147 return new ActionListener() {
148
149 public void actionPerformed(ActionEvent e) {
150
151 Container source = (Container) e.getSource();
152
153 Container a = SwingUtilities.getAncestorNamed(
154 CommonUI.EDIT_PANEL, source);
155 final Component ta = CommonUI.getComponentByName(a,
156 CommonUI.TEXT_COMP);
157
158 if (ta == null || !(ta instanceof JTextComponent)) {
159 return;
160 } else {
161
162 JButton jb = (JButton) source;
163 String cp = (String) jb.getClientProperty(CommonUI.MODE);
164
165 if (cp.equals(CommonUI.SAVE_MODE)) {
166
167 Runnable aRunnable = new Runnable() {
168 public void run() {
169 String text = ((JTextComponent) ta).getText();
170
171 String newHTML = null;
172 try {
173 newHTML = CommonUI.tidyHtml(text);
174 } catch (Exception e1) {
175 System.out.println("found it");
176 }
177
178 ((JTextComponent) ta).setText(newHTML);
179 System.out.println("TEXT" + text);
180 }
181 };
182 SwingUtilities.invokeLater(aRunnable);
183
184 }
185 }
186 }
187 };
188 }
189
190 public static String tidyHtml(String html, String name) {
191
192 Tidy tidy = new Tidy();
193 tidy.setXHTML(true);
194
195
196
197
198
199
200 html = html.replaceAll("--!>", "-->");
201
202
203 if (name != null) {
204 tidy.setInputStreamName(name);
205 }
206
207 StringWriter errorOut = new StringWriter();
208 tidy.setErrout(new PrintWriter(errorOut));
209
210 ByteArrayOutputStream baos = new ByteArrayOutputStream();
211
212 InputStream inputStream = IOUtils.toInputStream(html);
213 tidy.parse(inputStream, baos);
214
215 String tidyHTML = baos.toString();
216
217 IOUtils.closeQuietly(inputStream);
218 IOUtils.closeQuietly(baos);
219
220 if (tidy.getParseErrors() > 0) {
221
222
223
224
225 return "<html><body><h2>Errors</h2>\n"
226 + "<p>This page is printed if there is an error in the page that "
227 + "cannot be automatically fixed. Check for lines with <b>Error</b> in them.</p>\n"
228 + errorOut.toString().replaceAll("&", "&").replaceAll(
229 "<", "<").replaceAll(">", ">").replaceAll(
230 "\n", "<br/>") + "</body></html>";
231 }
232
233
234 System.out.println(errorOut.toString());
235
236 return tidyHTML;
237
238 }
239
240 public static Component getComponentByName(Container container, String name) {
241
242 if (container.getName() != null && container.getName().equals(name)) {
243 return container;
244 }
245
246 for (int i = 0; i < container.getComponentCount(); i++) {
247
248 Container c = (Container) container.getComponent(i);
249
250 Component n = getComponentByName(c, name);
251
252 if (n != null)
253 return n;
254
255 }
256 return null;
257 }
258
259 public static List getComponentsByName(Container container, String name, List<Component> cs) {
260
261 if( cs == null ) {
262 cs = new ArrayList<Component>();
263 }
264
265 if (container.getName() != null && container.getName().equals(name)) {
266 cs.add(container);
267 }
268
269 for (int i = 0; i < container.getComponents().length; i++) {
270
271 Container c = (Container) container.getComponent(i);
272
273 List css = getComponentsByName(c, name,cs);
274
275 if ( !css.isEmpty() )
276 return css;
277
278 }
279 return cs;
280 }
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304 public static JPanel createEditableTextarea(
305 final JTextComponent textComponent, Dimension dim) {
306
307 final JScrollPane sp = new JScrollPane(textComponent);
308 final ImageIcon editIcon = new ImageIcon(PasCommonIconProvider
309 .getImage("page_edit16.png"));
310 final ImageIcon saveIcon = new ImageIcon(PasCommonIconProvider
311 .getImage("page_save16.png"));
312
313 final Border whiteBorder = BorderFactory.createLineBorder(Color.white,
314 1);
315 final Border blackBorder = BorderFactory.createLineBorder(Color.black,
316 1);
317 JPanel t = new JPanel(new BorderLayout(0, 0));
318
319 if (textComponent instanceof JTextArea) {
320 textComponent.setBorder(whiteBorder);
321 ((JTextArea) textComponent).setWrapStyleWord(true);
322 ((JTextArea) textComponent).setLineWrap(true);
323 ((JTextArea) textComponent).setEditable(false);
324 sp.setName("AuthorNotesScrollPane");
325 sp
326 .setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
327 sp
328 .setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED);
329 sp.setPreferredSize(dim);
330 sp.setBorder(BorderFactory.createEmptyBorder());
331 textComponent.setBorder(BorderFactory.createEmptyBorder());
332 t.add(sp, BorderLayout.CENTER);
333 } else {
334 t.add(textComponent, BorderLayout.CENTER);
335 }
336
337 textComponent.setName(TEXT_COMP);
338 JButton editButton = new JButton("Edit");
339 editButton.setName(EDIT_BUTTON);
340 editButton.setHorizontalTextPosition(JButton.LEFT);
341 editButton.setIcon(editIcon);
342
343 final Color orgColor = textComponent.getBackground();
344 final Border orgBorder = textComponent.getBorder();
345 textComponent.setBorder(whiteBorder);
346
347 CommonUI.makeButtonTransparent(editButton);
348 editButton.setBorder(BorderFactory.createEmptyBorder(0, 4, 0, 4));
349 editButton.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
350 editButton.putClientProperty(MODE, EDIT_MODE);
351 editButton.addActionListener(new ActionListener() {
352
353 public void actionPerformed(ActionEvent e) {
354 JButton source = (JButton) e.getSource();
355 String clientProperty = (String) source.getClientProperty(MODE);
356
357 if (clientProperty.equals(EDIT_MODE)) {
358
359 textComponent.setEditable(true);
360 textComponent.setBackground(Color.white);
361
362 System.out.println("org border: " + orgBorder);
363 textComponent.setBorder(blackBorder);
364 if (textComponent instanceof JTextArea) {
365 sp.setBorder(BorderFactory.createEtchedBorder());
366 }
367 sp.setBorder(BorderFactory.createEmptyBorder());
368 source.putClientProperty(MODE, SAVE_MODE);
369 source.setText("Save");
370 source.setIcon(saveIcon);
371 source.revalidate();
372 textComponent.setFocusable(true);
373 textComponent.requestFocusInWindow();
374
375 } else if (clientProperty.equals(SAVE_MODE)) {
376
377 textComponent.setEditable(false);
378 textComponent.setBackground(orgColor);
379
380 textComponent.setBorder(whiteBorder);
381 if (textComponent instanceof JTextArea) {
382 sp.setBorder(BorderFactory.createEmptyBorder());
383 }
384 textComponent.setFocusable(false);
385
386 textComponent.transferFocus();
387
388 source.putClientProperty(MODE, EDIT_MODE);
389 source.setText("Edit");
390 source.setIcon(editIcon);
391 source.revalidate();
392 }
393
394 }
395
396 });
397
398 t.setOpaque(false);
399
400 if (textComponent instanceof JTextArea) {
401 JPanel a = new JPanel(new BorderLayout(0, 0));
402 a.setOpaque(false);
403 a.add(editButton, BorderLayout.NORTH);
404 t.add(a, BorderLayout.EAST);
405 } else {
406 t.add(editButton, BorderLayout.EAST);
407 }
408 t.setName(EDIT_PANEL);
409 return t;
410
411 }
412
413 public static Point centerDialogOnScreen(Rectangle bounds) {
414
415 Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
416
417 Point point = new Point((dim.width - bounds.width) / 2,
418 (dim.height - bounds.height) / 2);
419 return point;
420 }
421
422 public static Border createEtchedBorder() {
423 return BorderFactory.createEtchedBorder(EtchedBorder.LOWERED);
424 }
425
426 public static Border createLowerBannerGap() {
427 return BorderFactory.createEmptyBorder(4, 0, 0, 0);
428 }
429
430 public static void makeMultilineEditable(JTextComponent area) {
431 area.setEditable(true);
432 area.setEnabled(true);
433 if (area instanceof JTextArea) {
434 JTextArea ta = (JTextArea) area;
435 ta.setWrapStyleWord(true);
436 ta.setLineWrap(true);
437 }
438 }
439
440 public static void makeMultilineLabel(JTextComponent area) {
441 area.setEditable(false);
442 area.setEnabled(false);
443 if (area instanceof JTextArea) {
444 JTextArea ta = (JTextArea) area;
445
446 ta.setEnabled(true);
447 ta.setWrapStyleWord(true);
448 ta.setLineWrap(true);
449 }
450
451 }
452
453 private static final Border LINK_BORDER = BorderFactory.createEmptyBorder(
454 0, 0, 1, 0);
455 private static final Border HOVER_BORDER = BorderFactory.createMatteBorder(
456 0, 0, 1, 0, PasColors.htmlLinkButtonForegroundColor);
457 public static final String HTML_REGEX = "//<([^//>'\"]|\"[^\"]*\"|'[^']*')*//>";
458
459
460
461 public static Border createPurpleWhiteBorder() {
462 return BorderFactory.createCompoundBorder(BorderFactory
463 .createEmptyBorder(4, 4, 4, 4), BorderFactory.createLineBorder(
464 Color.WHITE, 2));
465 }
466
467 public static Border create4PEmptyBorder() {
468 return BorderFactory.createEmptyBorder(4, 4, 4, 4);
469 }
470
471 private static final MouseListener LINK_MOUSE_LISTENER = new MouseAdapter() {
472 public void mouseEntered(MouseEvent e) {
473 ((JComponent) e.getComponent()).setBorder(HOVER_BORDER);
474 }
475
476 public void mouseReleased(MouseEvent e) {
477 ((JComponent) e.getComponent()).setBorder(LINK_BORDER);
478 }
479
480 public void mouseExited(MouseEvent e) {
481 ((JComponent) e.getComponent()).setBorder(LINK_BORDER);
482 }
483 };
484
485 public static JButton makeLink(JButton button) {
486 button.setBorder(LINK_BORDER);
487 button.setForeground(PasColors.htmlLinkButtonForegroundColor);
488 button.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
489 button.setFocusPainted(false);
490 button.setRequestFocusEnabled(false);
491 button.addMouseListener(LINK_MOUSE_LISTENER);
492 return button;
493 }
494
495 public static JButton makeEnhancedButton(JButton button) {
496
497 Font font = button.getFont();
498 font = font.deriveFont(Font.BOLD, 11f);
499 button.setFont(font);
500 button.setOpaque(true);
501 button.setBackground(PasColors.enhancedButtonBackgroundColor);
502 button.setBorder(new InvertedBevelBorder(0,
503 PasColors.enhancedButtonLeftColor,
504 PasColors.enhancedButtonRightColor));
505 button.setForeground(PasColors.enhancedButtonForegroundColor);
506 button.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
507 button.setFocusPainted(false);
508 button.setRequestFocusEnabled(false);
509 return button;
510 }
511
512 public static void makeButtonWithHandCursor(JButton button) {
513 button.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
514 }
515
516 public static void applyDNDHack(JComponent comp) {
517 MouseListener dragListener = null;
518
519
520
521 try {
522 Class<?> clazz = Class
523 .forName("javax.swing.plaf.basic.BasicDragGestureRecognizer");
524 MouseListener[] mouseListeners = comp.getMouseListeners();
525 for (int i = 0; i < mouseListeners.length; i++) {
526 if (clazz.isAssignableFrom(mouseListeners[i].getClass())) {
527 dragListener = mouseListeners[i];
528 break;
529 }
530 }
531
532 if (dragListener != null) {
533 comp.removeMouseListener(dragListener);
534 comp
535 .removeMouseMotionListener((MouseMotionListener) dragListener);
536 comp.addMouseListener(dragListener);
537 comp.addMouseMotionListener((MouseMotionListener) dragListener);
538 }
539
540 } catch (ClassNotFoundException e) {
541 logger.severe("JComponent - : exception: " + e);
542 }
543
544 }
545
546 /***
547 * @param hintButton
548 */
549 public static JButton makeHintbutton(JButton hintButton) {
550 makeButtonTransparent(hintButton);
551 hintButton.setDisabledIcon(new ImageIcon(PasCommonIconProvider
552 .getImage("hintButtonDisabled.png")));
553 hintButton.setDisabledSelectedIcon(new ImageIcon(PasCommonIconProvider
554 .getImage("hintButtonDisabledSelected.png")));
555 hintButton.setIcon(new ImageIcon(PasCommonIconProvider
556 .getImage("hintButtonActive.png")));
557 hintButton.setRolloverIcon(new ImageIcon(PasCommonIconProvider
558 .getImage("hintButtonRollover.png")));
559 hintButton.setEnabled(false);
560
561 return hintButton;
562 }
563
564 public static JButton makeButtonTransparent(JButton button) {
565 button.setContentAreaFilled(false);
566 button.setBorder(BorderFactory.createEmptyBorder());
567 button.setRequestFocusEnabled(false);
568 button.setBorderPainted(false);
569 return button;
570 }
571
572 public static void addButtonRollover(JButton button, ImageIcon rollOverIcon) {
573 button.setOpaque(false);
574 button.setBorder(BorderFactory.createEmptyBorder());
575 button.setRolloverIcon(rollOverIcon);
576 }
577
578 public static JDialog showHintDialog(HintSet hintSet, JFrame frame) {
579 HintUI hintUI = new HintUI(hintSet);
580
581 JDialog dialog = PasDialogManager.showPasMessageDialog("Panda Hint",
582 null, null, hintUI, frame, true, true, false, hintUI
583 .getPreferredSize().width + 50, hintUI
584 .getPreferredSize().height + 100);
585
586 return dialog;
587 }
588
589 public static void modScrollPane(JScrollPane spane) {
590 spane.setOpaque(false);
591 spane
592 .setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
593 spane
594 .setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED);
595 spane.setBorder(BorderFactory.createEmptyBorder(0, 1, 0, 0));
596 spane.getVerticalScrollBar().getModel().setValue(
597 spane.getVerticalScrollBar().getModel().getMinimum());
598
599 }
600
601 /***
602 * Opens the native system file choosing dialog
603 * @return the file the user has chosen
604 */
605 public static File openDialog() {
606 return openDialog(null);
607 }
608
609 /***
610 * Opens the native system file choosing dialog
611 * @param the extension file type we are allowing the user to choose
612 * @return the file the user has chosen
613 */
614 public static File openDialog(String fileExtension) {
615
616 CCFileDialog fileDialog = new CCFileDialog();
617 CCFilenameFilter filenameFilter = new CCFilenameFilter();
618
619 if(fileExtension != null) {
620 filenameFilter.setAcceptableExtension(fileExtension);
621 }
622
623 fileDialog.setFilenameFilter(filenameFilter);
624 fileDialog.setFileSelectionMode(CCFileDialog.FILES_ONLY);
625
626
627 fileDialog.show();
628
629 String filePath = fileDialog.getDirectory() + fileDialog.getFile();
630 File file = new File(filePath);
631
632
633 fileName = fileDialog.getFile();
634
635 return file;
636 }
637
638 /***
639 * Opens the native system save file dialog
640 * @return the file to be saved
641 */
642 public static File saveDialog() {
643 CCFileDialog fileDialog = new CCFileDialog();
644
645 fileDialog.setMode(FileDialog.SAVE);
646 fileDialog.setFileSelectionMode(CCFileDialog.FILES_ONLY);
647
648
649 fileDialog.setFile(fileName);
650
651
652 fileDialog.show();
653 String filePath = fileDialog.getDirectory() + fileDialog.getFile();
654
655
656 setFileName(fileDialog.getFile());
657
658
659
660 File file = new File(filePath);
661 return file;
662 }
663
664 /***
665 * @return the fileName
666 */
667 public static String getFileName() {
668 return fileName;
669 }
670
671 /***
672 * @param fileName the fileName to set
673 */
674 public static void setFileName(String fileName) {
675 CommonUI.fileName = fileName;
676 }
677
678 /***
679 *
680 * @param url a path to a file
681 * @return the filename and extension without the directory
682 */
683 public static String extractFileName(String url) {
684 String fileName = "";
685
686 String os = System.getProperty("os.name");
687 char delimiter;
688 if(os != null && os.toLowerCase().contains("windows")) {
689
690 delimiter = '//';
691 } else {
692
693 delimiter = '/';
694 }
695
696
697 fileName = url.substring(url.lastIndexOf(delimiter) + 1);
698
699 return fileName;
700 }
701 }