1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.xnap.gui.menu;
21
22 import java.awt.event.ActionEvent;
23 import java.io.File;
24
25 import javax.swing.AbstractAction;
26 import javax.swing.AbstractButton;
27 import javax.swing.Action;
28 import javax.swing.ButtonGroup;
29 import javax.swing.JLabel;
30 import javax.swing.JMenu;
31 import javax.swing.JMenuItem;
32 import javax.swing.JRadioButtonMenuItem;
33
34 import org.xnap.XNap;
35 import org.xnap.event.ListEvent;
36 import org.xnap.event.ListListener;
37 import org.xnap.gui.FileProvider;
38 import org.xnap.gui.util.IconHelper;
39 import org.xnap.gui.viewer.Viewer;
40 import org.xnap.gui.viewer.ViewerFrontend;
41 import org.xnap.gui.viewer.ViewerManager;
42
43 public class ViewerMenu extends AbstractDynamicMenu implements ListListener
44 {
45
46
47
48
49
50 private FileProvider fp;
51 private ViewerFrontend vf;
52 private File currentFile = null;
53 private ButtonGroup bg;
54 private JMenu jmTryMenu = new JMenu(XNap.tr("Try Viewer"));
55
56
57
58 public ViewerMenu(FileProvider fp, ViewerFrontend vf)
59 {
60 super(XNap.tr("View With"));
61
62 this.fp = fp;
63 this.vf = vf;
64
65 setIcon(IconHelper.getMenuIcon("view_right.png"));
66
67 addSeparator();
68 add(jmTryMenu);
69 setUpTryMenu();
70
71 ViewerManager.getInstance().addListListener(this);
72 }
73
74
75
76 private void setUpTryMenu()
77 {
78 jmTryMenu.removeAll();
79 Viewer[] viewers = ViewerManager.getInstance().getViewers();
80
81 for (int i = 0; i < viewers.length; i++) {
82 jmTryMenu.add(new JMenuItem(new ViewerAction(viewers[i])));
83 }
84 }
85
86 public void itemAdded(ListEvent e)
87 {
88 setUpTryMenu();
89 }
90
91 public void itemRemoved(ListEvent e)
92 {
93 setUpTryMenu();
94 }
95
96 private void addViewers()
97 {
98 File files[] = fp.getFiles();
99
100 if (files == null || files.length > 1) {
101 addTemporary(new JLabel(XNap.tr("(Please select only one file)",
102 1)), 0);
103 jmTryMenu.setEnabled(false);
104 return;
105 }
106
107 jmTryMenu.setEnabled(true);
108 currentFile = files[0];
109
110 Viewer viewers[] = ViewerManager.getInstance().getViewers(currentFile);
111
112 bg = new ButtonGroup();
113 if (viewers != null) {
114 for (int i = 0; i < viewers.length; i++) {
115 Action ac = new ViewerAction(viewers[i]);
116 JRadioButtonMenuItem jcb = new JRadioButtonMenuItem(ac);
117 jcb.setSelected(i == 0);
118 bg.add(jcb);
119 addTemporary(jcb, i);
120 }
121 }
122 else {
123 addTemporary(new JLabel(XNap.tr("(No viewer available)", 1)), 0);
124 }
125 }
126
127 protected void willBecomeVisible()
128 {
129 removeAllTemporaries();
130 addViewers();
131 }
132
133 /***
134 * Wraps a viewer and propagates the display calls.
135 */
136 private class ViewerAction extends AbstractAction
137 {
138 private Viewer viewer;
139
140 public ViewerAction(Viewer viewer)
141 {
142 this.viewer = viewer;
143 putValue(Action.NAME, viewer.getName());
144 putValue(Action.SMALL_ICON, (viewer.getIcon() != null)
145 ? viewer.getIcon() : new IconHelper.EmptyIcon(16));
146 }
147
148 public void actionPerformed(ActionEvent event)
149 {
150 if (event.getSource() instanceof AbstractButton) {
151 bg.setSelected
152 (((AbstractButton)event.getSource()).getModel(), true);
153 }
154 ViewerManager.getInstance().setDefaultViewer
155 (currentFile, viewer);
156 vf.display(viewer, currentFile);
157 }
158 }
159
160 }