void createSection(Section section, FormToolkit toolkit) { // Toolbar buttons ToolBar toolbar = new ToolBar(section, SWT.FLAT); section.setTextClient(toolbar); fillToolBar(toolbar); Composite composite = toolkit.createComposite(section); section.setClient(composite); table = toolkit.createTable(composite, SWT.FULL_SELECTION | SWT.MULTI | SWT.BORDER); viewer = new TableViewer(table); viewer.setContentProvider(new ArrayContentProvider()); viewer.setLabelProvider(new VersionedClauseLabelProvider()); // Listeners viewer.addSelectionChangedListener( new ISelectionChangedListener() { public void selectionChanged(SelectionChangedEvent event) { ToolItem remove = getRemoveItemTool(); if (remove != null) remove.setEnabled(!viewer.getSelection().isEmpty()); } }); ViewerDropAdapter dropAdapter = new ViewerDropAdapter(viewer) { @Override public void dragEnter(DropTargetEvent event) { super.dragEnter(event); event.detail = DND.DROP_COPY; } @Override public boolean validateDrop(Object target, int operation, TransferData transferType) { if (FileTransfer.getInstance().isSupportedType(transferType)) { return true; } else if (ResourceTransfer.getInstance().isSupportedType(transferType)) { return true; } else { ISelection selection = LocalSelectionTransfer.getTransfer().getSelection(); if (selection.isEmpty() || !(selection instanceof IStructuredSelection)) { return false; } Iterator<?> iterator = ((IStructuredSelection) selection).iterator(); while (iterator.hasNext()) { Object element = iterator.next(); if (!(element instanceof RepositoryBundle) && !(element instanceof RepositoryBundleVersion) && !(element instanceof ProjectBundle)) { return false; } } return true; } } @Override public boolean performDrop(Object data) { if (data instanceof String[]) { return handleFileNameDrop((String[]) data); } else if (data instanceof IResource[]) { return handleResourceDrop((IResource[]) data); } else { return handleSelectionDrop(); } } private boolean handleResourceDrop(IResource[] resources) { File[] files = new File[resources.length]; for (int i = 0; i < resources.length; i++) { files[i] = resources[i].getLocation().toFile(); } return handleFileDrop(files); } private boolean handleFileNameDrop(String[] paths) { File[] files = new File[paths.length]; for (int i = 0; i < paths.length; i++) { files[i] = new File(paths[i]); } return handleFileDrop(files); } private boolean handleFileDrop(File[] files) { if (files.length > 0) { IPreferenceStore store = Plugin.getDefault().getPreferenceStore(); boolean hideWarning = store.getBoolean(Plugin.PREF_HIDE_WARNING_EXTERNAL_FILE); if (!hideWarning) { MessageDialogWithToggle dialog = MessageDialogWithToggle.openWarning( getSection().getShell(), "Add External Files", "External files cannot be directly added to a project, they must be added to a local repository first.", "Do not show this warning again.", false, null, null); if (Window.CANCEL == dialog.getReturnCode()) return false; if (dialog.getToggleState()) { store.setValue(Plugin.PREF_HIDE_WARNING_EXTERNAL_FILE, true); } } AddFilesToRepositoryWizard wizard = new AddFilesToRepositoryWizard(null, files); WizardDialog dialog = new WizardDialog(getSection().getShell(), wizard); if (Window.OK == dialog.open()) { List<Pair<String, String>> addingBundles = wizard.getSelectedBundles(); List<VersionedClause> addingClauses = new ArrayList<VersionedClause>(addingBundles.size()); for (Pair<String, String> addingBundle : addingBundles) { Map<String, String> attribs = new HashMap<String, String>(); attribs.put(Constants.VERSION_ATTRIBUTE, addingBundle.getSecond()); addingClauses.add(new VersionedClause(addingBundle.getFirst(), attribs)); } if (!addingClauses.isEmpty()) { bundles.addAll(addingClauses); viewer.add(addingClauses.toArray(new Object[addingClauses.size()])); markDirty(); } } return true; } return false; } private boolean handleSelectionDrop() { ISelection selection = LocalSelectionTransfer.getTransfer().getSelection(); if (selection.isEmpty() || !(selection instanceof IStructuredSelection)) { return false; } List<VersionedClause> adding = new LinkedList<VersionedClause>(); Iterator<?> iterator = ((IStructuredSelection) selection).iterator(); while (iterator.hasNext()) { Object item = iterator.next(); if (item instanceof RepositoryBundle) { String bsn = ((RepositoryBundle) item).getBsn(); adding.add(new VersionedClause(bsn, new HashMap<String, String>())); } else if (item instanceof RepositoryBundleVersion) { RepositoryBundleVersion bundleVersion = (RepositoryBundleVersion) item; Map<String, String> attribs = new HashMap<String, String>(); attribs.put(Constants.VERSION_ATTRIBUTE, bundleVersion.getVersion().toString()); adding.add(new VersionedClause(bundleVersion.getBundle().getBsn(), attribs)); } else if (item instanceof ProjectBundle) { String bsn = ((ProjectBundle) item).getBsn(); Map<String, String> attribs = new HashMap<String, String>(); attribs.put(Constants.VERSION_ATTRIBUTE, "snapshot"); adding.add(new VersionedClause(bsn, attribs)); } } if (!adding.isEmpty()) { bundles.addAll(adding); viewer.add(adding.toArray(new Object[adding.size()])); markDirty(); } return true; } }; dropAdapter.setFeedbackEnabled(false); dropAdapter.setExpandEnabled(false); viewer.addDropSupport( DND.DROP_COPY | DND.DROP_MOVE, new Transfer[] { LocalSelectionTransfer.getTransfer(), FileTransfer.getInstance(), ResourceTransfer.getInstance() }, dropAdapter); table.addKeyListener( new KeyAdapter() { @Override public void keyReleased(KeyEvent e) { if (e.character == SWT.DEL) { doRemove(); } else if (e.character == '+') {; doAdd(); } } }); // Layout GridLayout layout = new GridLayout(1, false); layout.horizontalSpacing = 0; layout.verticalSpacing = 0; layout.marginHeight = 0; layout.marginWidth = 0; composite.setLayout(layout); GridData gd = getTableLayoutData(); table.setLayoutData(gd); }
@Override public void createPartControl(Composite parent) { // CREATE CONTROLS Composite mainPanel = new Composite(parent, SWT.NONE); Control filterPanel = filterPart.createControl(mainPanel, 5, 5); Tree tree = new Tree(mainPanel, SWT.FULL_SELECTION | SWT.MULTI); filterPanel.setBackground(tree.getBackground()); viewer = new TreeViewer(tree); viewer.setContentProvider(contentProvider); ColumnViewerToolTipSupport.enableFor(viewer); viewer.setLabelProvider(new RepositoryTreeLabelProvider(false)); getViewSite().setSelectionProvider(viewer); createActions(); JpmPreferences jpmPrefs = new JpmPreferences(); final boolean showJpmOnClick = jpmPrefs.getBrowserSelection() != JpmPreferences.PREF_BROWSER_EXTERNAL; // LISTENERS filterPart.addPropertyChangeListener( new PropertyChangeListener() { @Override public void propertyChange(PropertyChangeEvent event) { String filter = (String) event.getNewValue(); updatedFilter(filter); } }); ViewerDropAdapter dropAdapter = new ViewerDropAdapter(viewer) { @Override public boolean validateDrop(Object target, int operation, TransferData transferType) { if (canDrop(target, transferType)) return true; boolean valid = false; if (target instanceof RepositoryPlugin) { if (((RepositoryPlugin) target).canWrite()) { if (URLTransfer.getInstance().isSupportedType(transferType)) return true; if (LocalSelectionTransfer.getTransfer().isSupportedType(transferType)) { ISelection selection = LocalSelectionTransfer.getTransfer().getSelection(); if (selection instanceof IStructuredSelection) { for (Iterator<?> iter = ((IStructuredSelection) selection).iterator(); iter.hasNext(); ) { Object element = iter.next(); if (element instanceof RepositoryBundle || element instanceof RepositoryBundleVersion) { valid = true; break; } if (element instanceof IFile) { valid = true; break; } if (element instanceof IAdaptable) { IFile file = (IFile) ((IAdaptable) element).getAdapter(IFile.class); if (file != null) { valid = true; break; } } } } } else { valid = true; } } } return valid; } @Override public void dragEnter(DropTargetEvent event) { super.dragEnter(event); event.detail = DND.DROP_COPY; } @Override public boolean performDrop(Object data) { if (RepositoriesView.this.performDrop( getCurrentTarget(), getCurrentEvent().currentDataType)) { viewer.refresh(getCurrentTarget(), true); return true; } boolean copied = false; if (URLTransfer.getInstance().isSupportedType(getCurrentEvent().currentDataType)) { try { URL url = new URL( (String) URLTransfer.getInstance() .nativeToJava(getCurrentEvent().currentDataType)); File tmp = File.createTempFile("dwnl", ".jar"); IO.copy(url, tmp); copied = addFilesToRepository((RepositoryPlugin) getCurrentTarget(), new File[] {tmp}); } catch (Exception e) { return false; } } else if (data instanceof String[]) { String[] paths = (String[]) data; File[] files = new File[paths.length]; for (int i = 0; i < paths.length; i++) { files[i] = new File(paths[i]); } copied = addFilesToRepository((RepositoryPlugin) getCurrentTarget(), files); } else if (data instanceof IResource[]) { IResource[] resources = (IResource[]) data; File[] files = new File[resources.length]; for (int i = 0; i < resources.length; i++) { files[i] = resources[i].getLocation().toFile(); } copied = addFilesToRepository((RepositoryPlugin) getCurrentTarget(), files); } else if (data instanceof IStructuredSelection) { File[] files = convertSelectionToFiles((IStructuredSelection) data); if (files != null && files.length > 0) copied = addFilesToRepository((RepositoryPlugin) getCurrentTarget(), files); } return copied; } }; dropAdapter.setFeedbackEnabled(false); dropAdapter.setExpandEnabled(false); viewer.addDropSupport( DND.DROP_COPY | DND.DROP_MOVE, new Transfer[] { URLTransfer.getInstance(), FileTransfer.getInstance(), ResourceTransfer.getInstance(), LocalSelectionTransfer.getTransfer() }, dropAdapter); viewer.addDragSupport( DND.DROP_COPY | DND.DROP_MOVE, new Transfer[] {LocalSelectionTransfer.getTransfer()}, new SelectionDragAdapter(viewer)); viewer.addSelectionChangedListener( new ISelectionChangedListener() { @Override public void selectionChanged(SelectionChangedEvent event) { boolean writableRepoSelected = false; IStructuredSelection selection = (IStructuredSelection) viewer.getSelection(); Object element = selection.getFirstElement(); if (element instanceof RepositoryPlugin) { RepositoryPlugin repo = (RepositoryPlugin) element; writableRepoSelected = repo.canWrite(); } addBundlesAction.setEnabled(writableRepoSelected); } }); tree.addMouseListener( new MouseAdapter() { @Override public void mouseUp(MouseEvent ev) { Object element = ((IStructuredSelection) viewer.getSelection()).getFirstElement(); if (element instanceof ContinueSearchElement) { try { getViewSite() .getPage() .showView( Plugin.JPM_BROWSER_VIEW_ID, null, showJpmOnClick ? IWorkbenchPage.VIEW_ACTIVATE : IWorkbenchPage.VIEW_CREATE); } catch (PartInitException e) { Plugin.getDefault().getLog().log(e.getStatus()); } } } }); viewer.addDoubleClickListener( new IDoubleClickListener() { @Override public void doubleClick(DoubleClickEvent event) { if (!event.getSelection().isEmpty()) { IStructuredSelection selection = (IStructuredSelection) event.getSelection(); Object element = selection.getFirstElement(); if (element instanceof IAdaptable) { URI uri = (URI) ((IAdaptable) element).getAdapter(URI.class); if (uri != null) { IWorkbenchPage page = getSite().getPage(); try { IFileStore fileStore = EFS.getLocalFileSystem().getStore(uri); IDE.openEditorOnFileStore(page, fileStore); } catch (PartInitException e) { logger.logError("Error opening editor for " + uri, e); } } } else if (element instanceof ContinueSearchElement) { ContinueSearchElement searchElement = (ContinueSearchElement) element; try { JpmPreferences jpmPrefs = new JpmPreferences(); if (jpmPrefs.getBrowserSelection() == JpmPreferences.PREF_BROWSER_EXTERNAL) getViewSite() .getWorkbenchWindow() .getWorkbench() .getBrowserSupport() .getExternalBrowser() .openURL(new URL("https://www.jpm4j.org/" + searchElement.getFilter())); else getViewSite() .getPage() .showView(Plugin.JPM_BROWSER_VIEW_ID, null, IWorkbenchPage.VIEW_VISIBLE); } catch (PartInitException e) { Plugin.getDefault().getLog().log(e.getStatus()); } catch (MalformedURLException e) { // ignore } } } } }); createContextMenu(); // LOAD Central.onWorkspaceInit( new Function<Workspace, Void>() { @Override public Void run(Workspace a) { final List<RepositoryPlugin> repositories = RepositoryUtils.listRepositories(true); SWTConcurrencyUtil.execForControl( viewer.getControl(), true, new Runnable() { @Override public void run() { viewer.setInput(repositories); } }); return null; } }); // LAYOUT GridLayout layout = new GridLayout(1, false); layout.horizontalSpacing = 0; layout.verticalSpacing = 0; layout.marginWidth = 0; layout.marginHeight = 0; mainPanel.setLayout(layout); tree.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true)); filterPanel.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false)); // Toolbar createActions(); fillToolBar(getViewSite().getActionBars().getToolBarManager()); // Register as repository listener registration = Activator.getDefault() .getBundleContext() .registerService(RepositoryListenerPlugin.class, this, null); }