1 19 20 package org.netbeans.modules.progress.ui; 21 22 import java.awt.Color ; 23 import java.awt.Component ; 24 import java.awt.Cursor ; 25 import java.awt.Dimension ; 26 import java.awt.Font ; 27 import java.awt.Image ; 28 import java.awt.LayoutManager ; 29 import java.awt.event.ActionEvent ; 30 import java.awt.event.FocusListener ; 31 import java.awt.event.KeyEvent ; 32 import java.awt.event.MouseAdapter ; 33 import java.awt.event.MouseEvent ; 34 import javax.swing.AbstractAction ; 35 import javax.swing.Action ; 36 import javax.swing.BorderFactory ; 37 import javax.swing.ImageIcon ; 38 import javax.swing.JButton ; 39 import javax.swing.JLabel ; 40 import javax.swing.JPanel ; 41 import javax.swing.JPopupMenu ; 42 import javax.swing.KeyStroke ; 43 import javax.swing.UIManager ; 44 import org.netbeans.progress.spi.InternalHandle; 45 import org.netbeans.progress.spi.ProgressEvent; 46 import org.openide.DialogDisplayer; 47 import org.openide.NotifyDescriptor; 48 import org.openide.util.NbBundle; 49 import org.openide.util.Utilities; 50 51 55 public class ListComponent extends JPanel { 56 private NbProgressBar bar; 57 private JLabel mainLabel; 58 private JLabel dynaLabel; 59 private JButton closeButton; 60 private InternalHandle handle; 61 private boolean watched; 62 private Action cancelAction; 63 private Color selectBgColor; 64 private Color selectFgColor; 65 private Color bgColor; 66 private Color fgColor; 67 private int mainHeight; 69 private int dynaHeight; 70 71 72 public ListComponent(InternalHandle hndl) { 73 setFocusable(true); 74 setRequestFocusEnabled(true); 75 mainLabel = new JLabel (); 77 dynaLabel = new JLabel (); 78 setOpaque(true); 81 dynaLabel.setFont(dynaLabel.getFont().deriveFont((float) (dynaLabel.getFont().getSize() - 2))); 82 bar = new NbProgressBar(); 83 handle = hndl; 84 Color bg = UIManager.getColor("nbProgressBar.popupText.background"); 85 if (bg != null) { 86 setBackground(bg); 87 mainLabel.setBackground(bg); 88 dynaLabel.setBackground(bg); 89 } 90 bgColor = getBackground(); 91 Color dynaFg = UIManager.getColor("nbProgressBar.popupDynaText.foreground"); 92 if (dynaFg != null) { 93 dynaLabel.setForeground(dynaFg); 94 } 95 fgColor = UIManager.getColor("nbProgressBar.popupText.foreground"); 97 if (fgColor != null) { 98 mainLabel.setForeground(fgColor); 99 } 100 fgColor = mainLabel.getForeground(); 101 selectBgColor = UIManager.getColor("nbProgressBar.popupText.selectBackground"); 102 if (selectBgColor == null) { 103 selectBgColor = UIManager.getColor("List.selectionBackground"); 104 } 105 selectFgColor = UIManager.getColor("nbProgressBar.popupText.selectForeground"); 106 if (selectFgColor == null) { 107 selectFgColor = UIManager.getColor("List.selectionForeground"); 108 } 109 bar.setToolTipText(NbBundle.getMessage(ListComponent.class, "ListComponent.bar.tooltip")); 110 bar.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR)); 111 mainLabel.setText("XYZ"); 113 dynaLabel.setText("XYZ"); 114 mainHeight = mainLabel.getPreferredSize().height; 115 dynaHeight = dynaLabel.getPreferredSize().height; 116 mainLabel.setText(null); 117 dynaLabel.setText(null); 118 120 setLayout(new CustomLayout()); 121 add(mainLabel); 122 add(bar); 123 MListener list = new MListener(); 124 if (handle.isAllowCancel()) { 125 cancelAction = new CancelAction(false); 126 closeButton = new JButton (cancelAction); 127 closeButton.setBorderPainted(false); 128 closeButton.setBorder(BorderFactory.createEmptyBorder()); 129 closeButton.setOpaque(false); 130 closeButton.setContentAreaFilled(false); 131 closeButton.setFocusable(false); 132 133 Image img = (Image )UIManager.get("nb.progress.cancel.icon"); 134 if( null != img ) { 135 closeButton.setIcon( new ImageIcon ( img ) ); 136 } 137 img = (Image )UIManager.get("nb.progress.cancel.icon.mouseover"); 138 if( null != img ) { 139 closeButton.setRolloverEnabled(true); 140 closeButton.setRolloverIcon( new ImageIcon ( img ) ); 141 } 142 img = (Image )UIManager.get("nb.progress.cancel.icon.pressed"); 143 if( null != img ) { 144 closeButton.setPressedIcon( new ImageIcon ( img ) ); 145 } 146 147 closeButton.setToolTipText(NbBundle.getMessage(ListComponent.class, "ListComponent.btnClose.tooltip")); 148 add(closeButton); 149 if (handle.getState() != InternalHandle.STATE_RUNNING) { 150 closeButton.setEnabled(false); 151 } 152 } 153 add(dynaLabel); 154 setBorder(BorderFactory.createEmptyBorder()); 155 addMouseListener(list); 156 bar.addMouseListener(list); 157 mainLabel.addMouseListener(list); 158 dynaLabel.addMouseListener(list); 159 if (handle.isAllowCancel()) { 160 closeButton.addMouseListener(list); 161 } 162 163 mainLabel.setText(handle.getDisplayName()); 164 NbProgressBar.setupBar(handle, bar); 165 addFocusListener(new FocusListener () { 166 public void focusGained(java.awt.event.FocusEvent e) { 167 if (!e.isTemporary()) { 168 setBackground(selectBgColor); 169 mainLabel.setBackground(selectBgColor); 170 dynaLabel.setBackground(selectBgColor); 171 mainLabel.setForeground(selectFgColor); 172 scrollRectToVisible(getBounds()); 175 } 176 } 177 178 public void focusLost(java.awt.event.FocusEvent e) { 179 if (!e.isTemporary()) { 180 setBackground(bgColor); 181 mainLabel.setBackground(bgColor); 182 dynaLabel.setBackground(bgColor); 183 mainLabel.setForeground(fgColor); 184 187 } 188 } 189 190 }); 191 192 } 193 194 195 Action getCancelAction() { 196 return cancelAction; 197 } 198 199 InternalHandle getHandle() { 200 return handle; 201 } 202 203 void processProgressEvent(ProgressEvent event) { 204 if (event.getType() == ProgressEvent.TYPE_PROGRESS || 205 event.getType() == ProgressEvent.TYPE_SWITCH || 206 event.getType() == ProgressEvent.TYPE_SILENT) { 207 if (event.getSource() != handle) { 208 throw new IllegalStateException (); 209 } 210 if (event.isSwitched()) { 211 NbProgressBar.setupBar(event.getSource(), bar); 212 } 213 if (event.getWorkunitsDone() > 0) { 214 bar.setValue(event.getWorkunitsDone()); 215 } 216 bar.setString(StatusLineComponent.getBarString(event.getPercentageDone(), event.getEstimatedCompletion())); 217 if (event.getMessage() != null) { 218 dynaLabel.setText(event.getMessage()); 219 } 220 if (event.getSource().getState() == InternalHandle.STATE_REQUEST_STOP) { 221 closeButton.setEnabled(false); 222 } 223 if (event.getDisplayName() != null) { 224 mainLabel.setText(event.getDisplayName()); 225 } 226 } else { 227 throw new IllegalStateException (); 228 } 229 } 230 231 void markAsActive(boolean sel) { 232 if (sel == watched) { 233 return; 234 } 235 watched = sel; 236 if (sel) { 237 mainLabel.setFont(mainLabel.getFont().deriveFont(Font.BOLD)); 238 } else { 239 mainLabel.setFont(mainLabel.getFont().deriveFont(Font.PLAIN)); 240 } 241 if (mainLabel.isVisible()) { 242 mainLabel.repaint(); 243 } 244 } 245 246 private class MListener extends MouseAdapter { 247 public void mouseClicked(MouseEvent e) { 248 if (e.getSource() == bar) { 249 handle.requestExplicitSelection(); 250 } 252 if (e.getClickCount() > 1 && (e.getSource() == mainLabel || e.getSource() == dynaLabel)) { 253 handle.requestView(); 254 } 255 if (e.getButton() != e.BUTTON1) { 256 showMenu(e); 257 } else { 258 ListComponent.this.requestFocus(); 259 } 260 261 } 263 } 264 265 private void showMenu(MouseEvent e) { 266 JPopupMenu popup = new JPopupMenu (); 267 popup.setName("progresspopup"); 269 popup.add(new ViewAction()); 270 popup.add(new WatchAction()); 271 popup.add(new CancelAction(true)); 272 popup.show((Component )e.getSource(), e.getX(), e.getY()); 273 } 274 275 private class CancelAction extends AbstractAction { 276 CancelAction(boolean text) { 277 if (text) { 278 putValue(Action.NAME, NbBundle.getMessage(ListComponent.class, "StatusLineComponent.Cancel")); 279 putValue(Action.ACCELERATOR_KEY, KeyStroke.getKeyStroke(KeyEvent.VK_DELETE, 0)); 280 } else { 281 Image icon = (Image )UIManager.get("nb.progress.cancel.icon"); 282 if (icon == null) { 283 icon = Utilities.loadImage("org/netbeans/progress/module/resources/buton.png"); 285 } 286 putValue(Action.SMALL_ICON, new ImageIcon (icon)); 287 } 288 setEnabled(handle == null ? false : handle.isAllowCancel()); 289 } 290 291 public void actionPerformed(ActionEvent actionEvent) { 292 if (handle.getState() == InternalHandle.STATE_RUNNING) { 293 String message = NbBundle.getMessage(ListComponent.class, "Cancel_Question", handle.getDisplayName()); 294 String title = NbBundle.getMessage(ListComponent.class, "Cancel_Question_Title"); 295 NotifyDescriptor dd = new NotifyDescriptor(message, title, 296 NotifyDescriptor.YES_NO_OPTION, 297 NotifyDescriptor.QUESTION_MESSAGE, null, null); 298 Object retType = DialogDisplayer.getDefault().notify(dd); 299 if (retType == NotifyDescriptor.YES_OPTION) { 300 handle.requestCancel(); 301 } 302 } 303 } 304 } 305 306 private class ViewAction extends AbstractAction { 307 public ViewAction() { 308 putValue(Action.NAME, NbBundle.getMessage(ListComponent.class, "StatusLineComponent.View")); 309 setEnabled(handle == null ? false : handle.isAllowView()); 310 putValue(Action.ACCELERATOR_KEY, KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0)); 311 } 312 public void actionPerformed(ActionEvent actionEvent) { 313 if (handle != null) { 314 handle.requestView(); 315 } 316 } 317 } 318 319 private class WatchAction extends AbstractAction { 320 public WatchAction() { 321 putValue(Action.NAME, NbBundle.getMessage(ListComponent.class, "ListComponent.Watch")); 322 putValue(Action.ACCELERATOR_KEY, KeyStroke.getKeyStroke(KeyEvent.VK_SPACE, 0)); 323 setEnabled(true); 324 } 325 public void actionPerformed(ActionEvent actionEvent) { 326 if (handle != null) { 327 handle.requestExplicitSelection(); 328 } 329 } 330 } 331 332 333 private static final int UPPERMARGIN = 3; 334 private static final int LEFTMARGIN = 2; 335 private static final int BOTTOMMARGIN = 2; 336 private static final int BETWEENTEXTMARGIN = 3; 337 338 static final int ITEM_WIDTH = 400; 339 340 private class CustomLayout implements LayoutManager { 341 342 351 public void addLayoutComponent(String name, java.awt.Component comp) { 352 } 353 354 361 public Dimension preferredLayoutSize(java.awt.Container parent) { 362 int height = UPPERMARGIN + mainHeight + BETWEENTEXTMARGIN + dynaHeight + BOTTOMMARGIN; 363 return new Dimension (ITEM_WIDTH, height); 364 } 365 366 371 public void layoutContainer(java.awt.Container parent) { 372 int parentWidth = parent.getWidth(); 373 int parentHeight = parent.getHeight(); 374 int offset = parentWidth - 18; 375 if (closeButton != null) { 376 closeButton.setBounds(offset, UPPERMARGIN, 18, mainHeight); 377 } 378 int barOffset = offset - (ITEM_WIDTH / 3); 380 bar.setBounds(barOffset, UPPERMARGIN, offset - barOffset, mainHeight); 381 mainLabel.setBounds(LEFTMARGIN, UPPERMARGIN, barOffset - LEFTMARGIN, mainHeight); 382 dynaLabel.setBounds(LEFTMARGIN, mainHeight + UPPERMARGIN + BETWEENTEXTMARGIN, 383 parentWidth - LEFTMARGIN, dynaHeight); 384 } 385 386 393 public Dimension minimumLayoutSize(java.awt.Container parent) { 394 return preferredLayoutSize(parent); 395 } 396 397 401 public void removeLayoutComponent(java.awt.Component comp) { 402 } 403 404 405 } 406 } | Popular Tags |