📄 labelsview.java
字号:
/* * Copyright 2006 Marcel Schoffelmeer * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */package com.s10r.manager.view;import java.util.List;import org.eclipse.swt.SWT;import org.eclipse.swt.layout.FillLayout;import org.eclipse.swt.layout.GridData;import org.eclipse.swt.widgets.Composite;import org.eclipse.swt.widgets.Event;import org.eclipse.swt.widgets.Listener;import org.eclipse.swt.widgets.Table;import org.eclipse.swt.widgets.TableColumn;import org.eclipse.swt.widgets.TableItem;import org.eclipse.ui.IEditorPart;import org.eclipse.ui.IPartListener;import org.eclipse.ui.IWorkbenchPage;import org.eclipse.ui.IWorkbenchPart;import org.eclipse.ui.IWorkbenchWindow;import org.eclipse.ui.part.ViewPart;import com.s10r.manager.EntryEditor;import com.s10r.manager.model.ItemLabel;import com.s10r.util.Observer;import com.s10r.util.UpdateEvent;public class LabelsView extends ViewPart implements IPartListener, Observer{ private static final String CLEAR_FILTER = "<clear filter>"; public static final String ID = "manager_rcp.labels"; private EntryEditor activeEditor; private Table table; public LabelsView() { } /** * Convenience method that checks for nulls along the way to calling * getActiveEditor. * * @return */ private IEditorPart getActiveEditor() { // Looking through the Eclipse src, it seems a practive to check // for null, especially for the getActivePage returns IWorkbenchWindow wb = getSite().getWorkbenchWindow(); if (wb != null) { IWorkbenchPage page = wb.getActivePage(); if (page != null) { return page.getActiveEditor(); } } return null; } @Override public void createPartControl(Composite parent) { parent.setLayout(new FillLayout()); createTable(parent); // listen for part selection changes so we can update this view // (with the labels of the active part) getSite().getWorkbenchWindow().getPartService().addPartListener(this); // search for the open active editor since if we opened after the // editor was opened, we do not get the IPartListener callback IEditorPart part = getActiveEditor(); if (part != null) { saveActiveEditor(part); } } private void createTable(Composite comp) { GridData data = new GridData(GridData.FILL, GridData.FILL, true, true); data.horizontalSpan = 2; table = new Table(comp, SWT.BORDER | SWT.FULL_SELECTION); table.setLayoutData(data); table.setHeaderVisible(true); TableColumn c1 = new TableColumn(table, SWT.NONE); c1.setText("Label"); c1.setWidth(100); table.addListener(SWT.Selection, new Listener() { public void handleEvent(Event event) { TableItem item = (TableItem) event.item; ItemLabel label = (ItemLabel) item.getData(); if (item.getText().equals(CLEAR_FILTER)) { activeEditor.filterOnLabel(null); } else { if (activeEditor != null) { activeEditor.filterOnLabel(label); } } } }); } @Override public void setFocus() { } public void partActivated(IWorkbenchPart part) { System.out.println("LabelsView.partActivated"); saveActiveEditor(part); } public void partBroughtToTop(IWorkbenchPart part) { // TODO Auto-generated method stub } public void partClosed(IWorkbenchPart part) { // Note: partClosed is only called when the part is closed accross // all perspectives! System.out.println("LabelsView.partClosed"); if (part == activeEditor) { System.out .println("LabelsView.clearActiveEditor: clearing activeEditor"); activeEditor = null; table.removeAll(); } else if (part == this) { // view closed: remove the filter on the editor if any if (activeEditor != null) { activeEditor.removeItemObserver(this); System.out.println(" clearing editor filter"); activeEditor.filterOnLabel(null); } } } public void partDeactivated(IWorkbenchPart part) { // also called when selecting other views: hold on to the editr so // we can filter the list based on label selections. } public void partOpened(IWorkbenchPart part) { saveActiveEditor(part); } private void saveActiveEditor(IWorkbenchPart part) { if (part instanceof EntryEditor) { if (activeEditor != null) { activeEditor.removeItemObserver(this); } activeEditor = (EntryEditor) part; activeEditor.addItemObserver(this); updateViewLabels(); } } private void updateViewLabels() { List<ItemLabel> labels = activeEditor.getLabels(); table.removeAll(); for (ItemLabel label : labels) { TableItem ti = new TableItem(table, SWT.NONE); ti.setText(label.getName()); ti.setData(label); } TableItem ti = new TableItem(table, SWT.NONE); ti.setText(CLEAR_FILTER); } @Override public void dispose() { if (activeEditor != null) { activeEditor.removeItemObserver(this); } getSite().getWorkbenchWindow().getPartService() .removePartListener(this); super.dispose(); } public void update(UpdateEvent event) { // anything happen to labels: update the view if (event.getTargetObject() instanceof ItemLabel) { updateViewLabels(); } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -