1 19 20 package org.netbeans.modules.diff.builtin; 21 22 import java.awt.*; 23 import java.beans.PropertyDescriptor ; 26 import java.beans.PropertyEditorManager ; 27 import java.io.FileNotFoundException ; 28 import java.io.Reader ; 29 import java.io.IOException ; 30 import java.io.InterruptedIOException ; 31 32 import org.netbeans.api.progress.ProgressHandle; 33 import org.netbeans.api.progress.ProgressHandleFactory; 34 35 import org.openide.explorer.propertysheet.ExPropertyEditor; 36 import org.openide.explorer.propertysheet.PropertyModel; 37 import org.openide.explorer.propertysheet.PropertyPanel; 38 import org.openide.explorer.propertysheet.DefaultPropertyModel; 39 import org.openide.filesystems.FileObject; 40 import org.openide.filesystems.FileSystem; 41 import org.openide.loaders.DataFolder; 42 import org.openide.loaders.DataObject; 43 import org.openide.loaders.DataObjectNotFoundException; 44 import org.openide.nodes.Node; 45 import org.openide.windows.TopComponent; 46 47 import org.netbeans.api.diff.Difference; 48 import org.netbeans.spi.diff.*; 49 import org.openide.loaders.InstanceDataObject; 50 import org.openide.util.NbBundle; 51 import org.openide.util.RequestProcessor; 52 import org.openide.util.Task; 53 import org.openide.ErrorManager; 54 55 import javax.swing.*; 56 57 61 public class DiffPresenter extends javax.swing.JPanel { 62 63 public static final String PROP_PROVIDER = "provider"; public static final String PROP_VISUALIZER = "visualizer"; 66 public static final String PROP_TOOLBAR = "DiffPresenter.toolbarPanel"; 68 private DiffPresenter.Info diffInfo; 69 private DiffProvider defaultProvider; 70 private DiffVisualizer defaultVisualizer; 71 private JComponent progressPanel; 72 73 77 private final RequestProcessor diffRP = new RequestProcessor("Diff", 1, true); 78 79 private RequestProcessor.Task computationTask = diffRP.post( new Runnable (){public void run(){}} 81 ); 82 83 private boolean added; 84 85 89 public DiffPresenter() { 90 String label = NbBundle.getMessage(DiffPresenter.class, "diff.prog"); 91 ProgressHandle progress = ProgressHandleFactory.createHandle(label); 92 progressPanel = ProgressHandleFactory.createProgressComponent(progress); 93 add(progressPanel); 94 progress.start(); 95 } 96 97 98 public DiffPresenter(DiffPresenter.Info diffInfo) { 99 initWithDiffInfo(diffInfo); 100 } 101 102 103 public final void initWithDiffInfo(DiffPresenter.Info diffInfo) { 104 assert this.diffInfo == null; 105 this.diffInfo = diffInfo; 106 if (progressPanel != null) { 107 remove(progressPanel); 108 } 109 initComponents(); 110 initMyComponents(); 111 providerLabel.getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getMessage(DiffPresenter.class, "ACS_ProviderA11yDesc")); visualizerLabel.getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getMessage(DiffPresenter.class, "ACS_VisualizerA11yDesc")); } 114 115 120 private void initComponents() { 122 java.awt.GridBagConstraints gridBagConstraints; 123 124 jPanel1 = new javax.swing.JPanel (); 125 toolbarPanel = new javax.swing.JPanel (); 126 servicesPanel = new javax.swing.JPanel (); 127 providerLabel = new javax.swing.JLabel (); 128 visualizerLabel = new javax.swing.JLabel (); 129 visualizerPanel = new javax.swing.JPanel (); 130 131 setLayout(new java.awt.GridBagLayout ()); 132 133 toolbarPanel.setLayout(new java.awt.FlowLayout (java.awt.FlowLayout.CENTER, 0, 0)); 134 135 toolbarPanel.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 0, 0, 0)); 136 gridBagConstraints = new java.awt.GridBagConstraints (); 137 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; 138 add(toolbarPanel, gridBagConstraints); 139 140 servicesPanel.setLayout(new java.awt.GridBagLayout ()); 141 142 org.openide.awt.Mnemonics.setLocalizedText(providerLabel, org.openide.util.NbBundle.getMessage(DiffPresenter.class, "LBL_Provider")); 143 gridBagConstraints = new java.awt.GridBagConstraints (); 144 gridBagConstraints.gridx = 0; 145 gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST; 146 gridBagConstraints.insets = new java.awt.Insets (0, 0, 0, 5); 147 servicesPanel.add(providerLabel, gridBagConstraints); 148 149 org.openide.awt.Mnemonics.setLocalizedText(visualizerLabel, org.openide.util.NbBundle.getMessage(DiffPresenter.class, "LBL_Visualizer")); 150 gridBagConstraints = new java.awt.GridBagConstraints (); 151 gridBagConstraints.gridx = 2; 152 gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST; 153 gridBagConstraints.insets = new java.awt.Insets (0, 0, 0, 5); 154 servicesPanel.add(visualizerLabel, gridBagConstraints); 155 156 gridBagConstraints = new java.awt.GridBagConstraints (); 157 gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER; 158 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; 159 gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTH; 160 gridBagConstraints.weightx = 1.0; 161 add(servicesPanel, gridBagConstraints); 162 163 visualizerPanel.setLayout(new java.awt.BorderLayout ()); 164 165 visualizerPanel.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color (0, 0, 0))); 166 gridBagConstraints = new java.awt.GridBagConstraints (); 167 gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER; 168 gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH; 169 gridBagConstraints.weightx = 1.0; 170 gridBagConstraints.weighty = 1.0; 171 add(visualizerPanel, gridBagConstraints); 172 173 } 175 176 javax.swing.JPanel jPanel1; 178 javax.swing.JLabel providerLabel; 179 javax.swing.JPanel servicesPanel; 180 javax.swing.JPanel toolbarPanel; 181 javax.swing.JLabel visualizerLabel; 182 javax.swing.JPanel visualizerPanel; 183 185 private void initMyComponents() { 186 PropertyDescriptor pd; 187 PropertyModel model; 188 PropertyPanel panel; 189 java.awt.GridBagConstraints gridBagConstraints; 190 FileSystem dfs = org.openide.filesystems.Repository.getDefault().getDefaultFileSystem(); 191 FileObject services = dfs.findResource("Services"); 192 DataFolder df = DataFolder.findFolder(services); 193 Object editor = PropertyEditorManager.findEditor (Object .class); 194 if (diffInfo.isChooseProviders() && editor != null) { 195 try { 196 pd = new PropertyDescriptor (PROP_PROVIDER, getClass ()); 197 } catch (java.beans.IntrospectionException intrex) { 198 return ; 199 } 200 pd.setPropertyEditorClass (editor.getClass()); 201 pd.setValue ("superClass", DiffProvider.class); 203 pd.setValue ("suppressCustomEditor", Boolean.TRUE); 204 FileObject providersFO = services.getFileObject("DiffProviders"); 205 try { 206 DataObject providersDO = DataObject.find(providersFO); 207 Node providersNode = providersDO.getNodeDelegate(); 208 pd.setValue("node", providersNode); 209 } catch (DataObjectNotFoundException donfex) {} 210 pd.setValue(ExPropertyEditor.PROPERTY_HELP_ID, "org.netbeans.modules.diff.DiffPresenter.providers"); 211 model = new DefaultPropertyModel (this, pd); 212 panel = new PropertyPanel (model, PropertyPanel.PREF_INPUT_STATE); 213 214 gridBagConstraints = new java.awt.GridBagConstraints (); 215 gridBagConstraints.insets = new java.awt.Insets (0, 0, 0, 15); 216 gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST; 217 if (!diffInfo.isChooseVisualizers()) gridBagConstraints.weightx = 1.0; 218 gridBagConstraints.gridx = 1; 219 servicesPanel.add(panel, gridBagConstraints); 220 providerLabel.setLabelFor(panel); 221 panel.getAccessibleContext().setAccessibleName(org.openide.util.NbBundle.getMessage(DiffPresenter.class, "ACS_ProviderPropertyPanelA11yName")); panel.getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getMessage(DiffPresenter.class, "ACS_ProviderPropertyPanelA11yDesc")); } 224 if (diffInfo.isChooseVisualizers() && editor != null) { 225 try { 226 pd = new PropertyDescriptor (PROP_VISUALIZER, getClass ()); 227 } catch (java.beans.IntrospectionException intrex) { 228 return ; 229 } 230 pd.setPropertyEditorClass (editor.getClass()); 231 pd.setValue ("superClass", DiffVisualizer.class); 233 pd.setValue ("suppressCustomEditor", Boolean.TRUE); 234 FileObject visualizersFO = services.getFileObject("DiffVisualizers"); 235 try { 236 DataObject visualizersDO = DataObject.find(visualizersFO); 237 Node visualizersNode = visualizersDO.getNodeDelegate(); 238 pd.setValue("node", visualizersNode); 239 } catch (DataObjectNotFoundException donfex) {} 240 pd.setValue(ExPropertyEditor.PROPERTY_HELP_ID, "org.netbeans.modules.diff.DiffPresenter.visualizers"); 241 model = new DefaultPropertyModel (this, pd); 242 panel = new PropertyPanel (model, PropertyPanel.PREF_INPUT_STATE); 243 panel.getAccessibleContext().setAccessibleName(org.openide.util.NbBundle.getMessage(DiffPresenter.class, "ACS_VisualizerPropertyPanelA11yName")); panel.getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getMessage(DiffPresenter.class, "ACS_VisualizerPropertyPanelA11yDesc")); 246 gridBagConstraints = new java.awt.GridBagConstraints (); 247 gridBagConstraints.insets = new java.awt.Insets (0, 0, 0, 5); 248 gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST; 249 gridBagConstraints.weightx = 1.0; 250 gridBagConstraints.gridx = 3; 251 servicesPanel.add(panel, gridBagConstraints); 252 visualizerLabel.setLabelFor(panel); 253 } 254 providerLabel.setVisible(diffInfo.isChooseProviders() && editor != null); 255 visualizerLabel.setVisible(diffInfo.isChooseVisualizers() && editor != null); 256 servicesPanel.setVisible((diffInfo.isChooseProviders() || diffInfo.isChooseVisualizers()) && editor != null); 257 } 258 259 public DiffProvider getProvider() { 260 return defaultProvider; 261 } 262 263 264 public void setProvider(DiffProvider p) { 265 this.defaultProvider = (DiffProvider) p; 266 267 if (added) { 268 asyncDiff((DiffProvider) p, defaultVisualizer); 269 setDefaultDiffService(p, "Services/DiffProviders"); } 271 } 272 273 public DiffVisualizer getVisualizer() { 274 return defaultVisualizer; 275 } 276 277 278 public void setVisualizer(DiffVisualizer v) { 279 this.defaultVisualizer = (DiffVisualizer) v; 280 281 if (added) { 282 asyncDiff(defaultProvider, (DiffVisualizer) v); 283 setDefaultDiffService(v, "Services/DiffVisualizers"); } 285 } 286 287 private static void setDefaultDiffService(Object ds, String folder) { 288 FileSystem dfs = org.openide.filesystems.Repository.getDefault().getDefaultFileSystem(); 290 FileObject services = dfs.findResource(folder); 291 DataFolder df = DataFolder.findFolder(services); 292 DataObject[] children = df.getChildren(); 293 for (int i = 0; i < children.length; i++) { 295 if (children[i] instanceof InstanceDataObject) { 296 InstanceDataObject ido = (InstanceDataObject) children[i]; 297 if (ido.instanceOf(ds.getClass())) { 298 try { 300 if (ds.equals(ido.instanceCreate())) { 301 df.setOrder(new DataObject[] { ido }); 303 break; 304 } 305 } catch (java.io.IOException ioex) { 306 } catch (ClassNotFoundException cnfex) {} 307 } 308 } 309 } 310 } 311 312 313 330 331 332 public void addNotify() { 333 super.addNotify(); 334 added = true; 335 asyncDiff(defaultProvider, defaultVisualizer); 336 } 337 338 339 public void removeNotify() { 340 super.removeNotify(); 341 computationTask.cancel(); 342 } 343 344 347 private synchronized void asyncDiff(final DiffProvider p, final DiffVisualizer v) { 348 if (v == null) { 349 return; 350 } 351 352 Difference[] diffs; 353 if (p != null) { 354 diffs = diffInfo.getInitialDifferences(); 355 if (diffs == null) { 356 JPanel panel = new JPanel(); 357 panel.setLayout(new BorderLayout()); 358 String message = NbBundle.getMessage(DiffPresenter.class, "BK0001"); 359 JLabel label = new JLabel(message); 360 label.setHorizontalAlignment(JLabel.CENTER); 361 panel.add(label, BorderLayout.CENTER); 362 setVisualizer(panel); 363 } 364 } else { 365 diffs = diffInfo.getDifferences(); 366 } 367 368 final Difference[] fdiffs = diffs; 369 Runnable computation = new Runnable () { 370 public void run() { 371 try { 372 Difference[] adiffs = fdiffs; 373 String message = NbBundle.getMessage(DiffPresenter.class, "BK0001"); 374 ProgressHandle ph = ProgressHandleFactory.createHandle(message); 375 if (adiffs == null) { 376 try { 377 ph.start(); 378 adiffs = p.computeDiff( 379 diffInfo.createFirstReader(), 380 diffInfo.createSecondReader() 381 ); 382 } finally { 383 ph.finish(); 384 } 385 } 386 387 if (adiffs == null) { 388 return; 389 } 390 391 final Difference[] fadiffs = adiffs; 392 javax.swing.SwingUtilities.invokeLater(new Runnable () { 393 public void run() { 394 try { 395 viewVisualizer(v, fadiffs); 396 } catch (IOException ioex) { 397 ErrorManager.getDefault().notify(ErrorManager.USER, ioex); 398 } 399 } 400 }); 401 402 } catch (InterruptedIOException ex) { 403 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ex); 404 } catch (IOException ex) { 405 ErrorManager.getDefault().notify(ErrorManager.USER, ex); 406 } 407 } 408 }; 409 410 computationTask.cancel(); 411 computationTask = diffRP.post(computation); 412 } 413 414 private void viewVisualizer(DiffVisualizer v, Difference[] diffs) throws IOException { 415 assert SwingUtilities.isEventDispatchThread(); 416 Component c = v.createView(diffs, diffInfo.getName1(), diffInfo.getTitle1(), 417 diffInfo.createFirstReader(), diffInfo.getName2(), diffInfo.getTitle2(), 418 diffInfo.createSecondReader(), diffInfo.getMimeType()); 419 setVisualizer((JComponent)c); 420 TopComponent tp = diffInfo.getPresentingComponent(); 421 if (tp != null) { 422 tp.setName(c.getName()); 423 if (c instanceof TopComponent) { 424 TopComponent vtp = (TopComponent) c; 425 tp.setToolTipText(vtp.getToolTipText()); 426 tp.setIcon(vtp.getIcon()); 427 } 428 } 429 c.requestFocus(); 430 } 431 432 private void setVisualizer(JComponent visualizer) { 433 visualizerPanel.removeAll(); 434 if (visualizer != null) { 435 toolbarPanel.removeAll(); 436 JComponent toolbar = (JComponent) visualizer.getClientProperty(PROP_TOOLBAR); 437 if (toolbar != null) { 438 toolbarPanel.add(toolbar); 439 } 440 visualizerPanel.add(visualizer, java.awt.BorderLayout.CENTER); 441 } 442 revalidate(); 443 repaint(); 444 } 445 446 449 public static abstract class Info extends Object { 450 451 private String name1; 452 private String name2; 453 private String title1; 454 private String title2; 455 private String mimeType; 456 private boolean chooseProviders; 457 private boolean chooseVisualizers; 458 private TopComponent tp; 459 460 public Info(String name1, String name2, String title1, String title2, 461 String mimeType, boolean chooseProviders, boolean chooseVisualizers) { 462 this.name1 = name1; 463 this.name2 = name2; 464 this.title1 = title1; 465 this.title2 = title2; 466 this.mimeType = mimeType; 467 this.chooseProviders = chooseProviders; 468 this.chooseVisualizers = chooseVisualizers; 469 } 470 471 public String getName1() { 472 return name1; 473 } 474 475 public String getName2() { 476 return name2; 477 } 478 479 public String getTitle1() { 480 return title1; 481 } 482 483 public String getTitle2() { 484 return title2; 485 } 486 487 public String getMimeType() { 488 return mimeType; 489 } 490 491 public boolean isChooseProviders() { 492 return chooseProviders; 493 } 494 495 public boolean isChooseVisualizers() { 496 return chooseVisualizers; 497 } 498 499 public Difference[] getDifferences() { 500 return null; 501 } 502 503 public Difference[] getInitialDifferences() { 504 return null; 505 } 506 507 public abstract Reader createFirstReader() throws FileNotFoundException ; 508 509 public abstract Reader createSecondReader() throws FileNotFoundException ; 510 511 public void setPresentingComponent(TopComponent tp) { 512 this.tp = tp; 513 } 514 515 public TopComponent getPresentingComponent() { 516 return tp; 517 } 518 519 } 520 521 } 522 | Popular Tags |