1 package org.robsite.extension.rss;
2
3 import java.awt.Component;
4 import java.awt.event.MouseAdapter;
5 import java.awt.event.MouseEvent;
6
7 import java.util.EventObject;
8
9 import javax.swing.JList;
10 import javax.swing.JTable;
11 import javax.swing.Timer;
12 import javax.swing.event.ChangeEvent;
13 import javax.swing.event.ChangeListener;
14
15 import oracle.ide.ContextMenu;
16 import oracle.ide.Ide;
17 import oracle.ide.IdeAction;
18 import oracle.ide.IdeContext;
19 import oracle.ide.addin.Context;
20 import oracle.ide.addin.ContextMenuListener;
21 import oracle.ide.dialogs.ExceptionDialog;
22 import oracle.ide.docking.DockableEvent;
23 import oracle.ide.docking.DockableWindow;
24 import oracle.ide.model.Element;
25
26 /***
27 * The RSS News dockable window.
28 *
29 * @author brian_duff@sourceforge.net
30 * @version $Revision: 1.1.1.1 $
31 */
32 public class NewsDockableWindow extends DockableWindow
33 {
34 private final NewsFeedPanel _panel = new NewsFeedPanel();
35 private boolean _isInitialized = false;
36
37 private ContextMenu _contextMenu;
38
39 private final ResHelper _res = new ResHelper( "NewsDockable." );
40
41 public NewsDockableWindow()
42 {
43 super( Ide.getMainWindow(), NewsFeedExtension.VIEW_ID );
44
45 ContextMenuMouseListener listener = new ContextMenuMouseListener();
46 _panel.getChannelList().addMouseListener( listener );
47 _panel.getItemTable().addMouseListener( listener );
48 }
49
50 public NewsFeedPanel getPanel()
51 {
52 return _panel;
53 }
54
55 public String getTabName()
56 {
57 return _res.getRes( "TabName" );
58 }
59
60 public String getTitleName()
61 {
62 return getTabName();
63 }
64
65 public Component getGUI()
66 {
67 return _panel;
68 }
69
70 public Context getContext( EventObject eo )
71 {
72 IdeContext context = new IdeContext();
73
74 context.setSelection( (Element[]) _panel.getSelectedItems() );
75 context.setEvent( eo );
76 return context;
77 }
78
79 public ContextMenu getContextMenu()
80 {
81 if ( _contextMenu == null )
82 {
83 _contextMenu = new ContextMenu();
84 _contextMenu.addContextMenuListener( new ContextMenuListener()
85 {
86 public void poppingUp( ContextMenu menu )
87 {
88 installContextMenuItems( menu );
89 }
90
91 public void poppingDown( ContextMenu menu )
92 {
93
94 }
95
96 public boolean handleDefaultAction( Context context )
97 {
98
99 IdeAction action = Commands.getAction( Commands.OPEN_IN_BROWSER );
100 action.updateAction( context );
101 if ( action.isEnabled() )
102 {
103 try
104 {
105 action.performAction( context );
106 return true;
107 }
108 catch ( Exception e )
109 {
110 ExceptionDialog.showExceptionDialog( context, e );
111 return false;
112 }
113 }
114
115 return false;
116 }
117 }
118 );
119 }
120 return _contextMenu;
121 }
122
123 private void installContextMenuItems( ContextMenu menu )
124 {
125 Context context = menu.getContext();
126 boolean itemsSelected = context.getSelection() != null &&
127 context.getSelection().length > 0;
128
129 float section = 1.0f;
130
131 if ( context.getEvent().getSource() == getPanel().getChannelList() )
132 {
133 if ( getPanel().getSelectedChannel() != null )
134 {
135 menu.add(
136 menu.createMenuItem( Commands.getAction( Commands.OPEN_IN_BROWSER ) ),
137 section
138 );
139 }
140 }
141 else if ( context.getEvent().getSource() == getPanel().getItemTable() )
142 {
143 if ( context.getSelection() != null && context.getSelection().length == 1 )
144 {
145 menu.add(
146 menu.createMenuItem( Commands.getAction( Commands.OPEN_IN_BROWSER ) ),
147 section
148 );
149 }
150 }
151
152 section++;
153
154 if ( itemsSelected )
155 {
156 menu.add(
157 menu.createMenuItem( Commands.getAction( Commands.MARK_AS_READ ) ),
158 section
159 );
160 }
161
162 }
163
164 public void stateChange( int newState )
165 {
166 if ( newState == CLOSED )
167 {
168 if ( _isInitialized )
169 {
170 NewsFeedExtension.get().setNewsDockable( null );
171
172 _isInitialized = false;
173 }
174 }
175
176 super.stateChange( newState );
177 }
178
179 public final void dockableShown( DockableEvent event )
180 {
181 super.dockableShown( event );
182
183 if ( !_isInitialized )
184 {
185 _panel.setChannels( NewsFeedExtension.get().getChannels() );
186 NewsFeedExtension.get().setNewsDockable( this );
187
188 _isInitialized = true;
189 }
190 }
191
192
193
194 private class ContextMenuMouseListener extends MouseAdapter
195 {
196 public void mouseClicked( MouseEvent me )
197 {
198
199 if ( me.getClickCount() == 2 && me.getButton() == MouseEvent.BUTTON1 )
200 {
201
202 if ( me.getSource() instanceof JList )
203 {
204 if ( ((JList)me.getSource()).locationToIndex( me.getPoint() ) < 0 )
205 {
206 return;
207 }
208 }
209 else if ( me.getSource() instanceof JTable )
210 {
211 if ( ((JTable)me.getSource()).rowAtPoint( me.getPoint() ) < 0 )
212 {
213 return;
214 }
215 }
216
217 getContextMenu().fireDefaultAction( getContext( me ) );
218
219 }
220 }
221
222 public void mouseReleased( MouseEvent me )
223 {
224 if ( me.isPopupTrigger() )
225 {
226
227 if ( me.getSource() instanceof JList )
228 {
229 JList list = (JList) me.getSource();
230 int index = list.locationToIndex( me.getPoint() );
231 if ( index >= 0 )
232 {
233 if ( !list.isSelectedIndex( index ) )
234 {
235 list.setSelectedIndex( index );
236 }
237 }
238 }
239 else if ( me.getSource() instanceof JTable )
240 {
241 JTable table = (JTable) me.getSource();
242 int index = table.rowAtPoint( me.getPoint() );
243 if ( index >= 0 )
244 {
245 if ( !table.isRowSelected( index ) )
246 {
247 table.setRowSelectionInterval( index, index );
248 }
249 }
250 }
251
252 getContextMenu().show( getContext( me ) );
253 }
254 }
255 }
256 }