public Composite create(Composite parentComp) { Composite comp = new Composite(parentComp, SWT.EMBEDDED); comp.setLayout(new MigLayout()); createGroup(comp, "车站信息浏览", "pos 0.5al 0.2al", null); return comp; }
/* (non-Javadoc) * @see org.eclipse.pde.internal.ui.editor.PDESection#createClient(org.eclipse.ui.forms.widgets.Section, org.eclipse.ui.forms.widgets.FormToolkit) */ protected void createClient(Section section, FormToolkit toolkit) { section.setLayout(FormLayoutFactory.createClearGridLayout(false, 1)); GridData sectionData = new GridData(GridData.FILL_BOTH); sectionData.verticalSpan = 2; section.setLayoutData(sectionData); Composite container = createClientContainer(section, 2, toolkit); createViewerPartControl(container, SWT.MULTI, 2, toolkit); container.setLayoutData(new GridData(GridData.FILL_BOTH)); createOptionalDependenciesButton(container); TablePart tablePart = getTablePart(); fPluginTable = tablePart.getTableViewer(); fPluginTable.setContentProvider(new ContentProvider()); fPluginTable.setLabelProvider(PDEPlugin.getDefault().getLabelProvider()); fPluginTable.setComparator( new ViewerComparator() { public int compare(Viewer viewer, Object e1, Object e2) { IProductPlugin p1 = (IProductPlugin) e1; IProductPlugin p2 = (IProductPlugin) e2; return super.compare(viewer, p1.getId(), p2.getId()); } }); GridData data = (GridData) tablePart.getControl().getLayoutData(); data.minimumWidth = 200; fPluginTable.setInput(getProduct()); tablePart.setButtonEnabled(0, isEditable()); tablePart.setButtonEnabled(1, isEditable()); tablePart.setButtonEnabled(2, isEditable()); // remove buttons will be updated on refresh tablePart.setButtonEnabled(5, isEditable()); toolkit.paintBordersFor(container); section.setClient(container); section.setText(PDEUIMessages.Product_PluginSection_title); section.setDescription(PDEUIMessages.Product_PluginSection_desc); getModel().addModelChangedListener(this); createSectionToolbar(section, toolkit); }
@Override protected Control createControl(Composite parent) { Composite propertyComposite = new Composite(parent, SWT.BORDER); GridLayout layout = new GridLayout(1, false); layout.marginWidth = layout.marginHeight = 0; propertyComposite.setLayout(layout); if (mPage instanceof Page) { ((Page) mPage) .init( new IPageSite() { public void registerContextMenu( String menuId, MenuManager menuManager, ISelectionProvider selectionProvider) {} public IActionBars getActionBars() { return null; } public IWorkbenchPage getPage() { return getWorkbenchWindow().getActivePage(); } public ISelectionProvider getSelectionProvider() { return null; } public Shell getShell() { return getWorkbenchWindow().getShell(); } public IWorkbenchWindow getWorkbenchWindow() { return PlatformUI.getWorkbench().getActiveWorkbenchWindow(); } public void setSelectionProvider(ISelectionProvider provider) {} @SuppressWarnings("unchecked") public Object getAdapter(Class adapter) { return null; } @SuppressWarnings("unchecked") public Object getService(Class api) { return null; } @SuppressWarnings("unchecked") public boolean hasService(Class api) { return false; } }); } if (mPage instanceof PropertySheetPage) { ((PropertySheetPage) mPage).setPropertySourceProvider(this); } mPage.createControl(propertyComposite); mPage.setActionBars(new DummyActionBars()); final Control control = mPage.getControl(); GridData data = new GridData(SWT.FILL, SWT.FILL, true, true); if (control instanceof Tree) { final Tree tree = (Tree) control; data.heightHint = tree.getItemHeight() * 13 + (tree.getLinesVisible() ? 12 * tree.getGridLineWidth() : 0) + (tree.getHeaderVisible() ? tree.getHeaderHeight() : 0) + 2 * tree.getBorderWidth() + (tree.getHorizontalBar() != null ? tree.getHorizontalBar().getSize().x : 0); tree.addControlListener( new ControlAdapter() { @Override public void controlResized(ControlEvent e) { Rectangle area = tree.getClientArea(); TreeColumn[] columns = tree.getColumns(); if (area.width > 0) { columns[0].setWidth(area.width * 40 / 100); columns[1].setWidth(area.width - columns[0].getWidth() - 4); } } }); } else if (control instanceof Composite) { control.addControlListener( new ControlAdapter() { @Override public void controlResized(ControlEvent e) { ((Composite) control).layout(true, true); } }); } control.setLayoutData(data); ISelection selection; if (mCurrentWidget == null) { Collection<InstallOptionsModelTypeDef> typeDefs = InstallOptionsModel.INSTANCE.getControlTypeDefs(); if (typeDefs.size() > 0) { InstallOptionsModelTypeDef typeDef = typeDefs.iterator().next(); InstallOptionsElementFactory factory = InstallOptionsElementFactory.getFactory(typeDef.getType()); mCurrentWidget = (InstallOptionsWidget) factory.getNewObject(); mDialog.addChild(mCurrentWidget); } } if (mCurrentWidget != null) { mCurrentWidget.addModelCommandListener(InstallOptionsWidgetEditorDialog.this); mCurrentWidget.addPropertyChangeListener(InstallOptionsWidgetEditorDialog.this); if (mCurrentWidget.getParent() != null) { mCurrentWidget.getParent().addPropertyChangeListener(InstallOptionsWidgetEditorDialog.this); } selection = new StructuredSelection(mCurrentWidget); } else { selection = StructuredSelection.EMPTY; } mPage.selectionChanged(null, selection); PlatformUI.getWorkbench().getHelpSystem().setHelp(mPage.getControl(), HELP_CONTEXT); PlatformUI.getWorkbench().getHelpSystem().setHelp(propertyComposite, HELP_CONTEXT); return propertyComposite; }
private void createComp(Composite group) { GridLayout layout = new GridLayout(1, false); layout.verticalSpacing = 10; group.setLayout(layout); // TableViewer是通过Table来布局的 // 制作表格 MULTI可多选 H_SCROLL有水平 滚动条、V_SCROLL有垂直滚动条、BORDER有边框、FULL_SELECTION整行选择 table = new Table(group, SWT.SINGLE | SWT.FULL_SELECTION | SWT.BORDER | SWT.VIRTUAL); // 注意此处的设置 TableLayout tableLayout = new TableLayout(); table.setLayout(tableLayout); // 指定Table单元格的宽度和高度 table.addListener( SWT.MeasureItem, new Listener() { // 向表格增加一个SWT.MeasureItem监听器,每当需要单元内容的大小的时候就会被调用。 public void handleEvent(Event event) { event.width = table.getGridLineWidth(); // 设置宽度 event.height = (int) Math.floor(event.gc.getFontMetrics().getHeight() * 1.5); // 设置高度为字体高度的1.5倍 } }); // 表格的视图 tableViewer = new TableViewer(table); // 标题和网格线可见 tableViewer.getTable().setLinesVisible(true); tableViewer.getTable().setHeaderVisible(true); // 设置填充 // GridData data = new GridData(SWT.LEFT,SWT.CENTER, true, false);//SWT.FILL, SWT.FILL, true, // false xbm GridData data = new GridData(GridData.FILL_BOTH); data.widthHint = 350; data.heightHint = 295; data.grabExcessHorizontalSpace = true; tableViewer.getTable().setLayoutData(data); // 表格的布局 // 创建表格列的标题 int width = 1; for (int i = 0; i < stationData.getColumnCount(); i++) { width = stationData.getColumnWidth(i); TableColumn column = new TableColumn(table, SWT.NONE); column.setWidth((int) (width * 8)); column.setText(stationData.getColumnHeads()[i]); // 设置表头 column.setAlignment(SWT.LEFT); // 对齐方式SWT.LEFT if (i == 0) // 站名 { // 列的选择事件 实现排序 column.addSelectionListener( new SelectionAdapter() { boolean sortType = true; // sortType记录上一次的排序方式,默认为升序 public void widgetSelected(SelectionEvent e) { sortType = !sortType; // 取反。下一次排序方式要和这一次的相反 tableViewer.setSorter(new StationSorter(sortType, stationData.columnHeads[0])); } }); } } /*tableLayout.addColumnData(new ColumnWeightData(8, 8, false));//设置列宽为8像素 TableColumn column_one = new TableColumn(table, SWT.NONE);//SWT.LEFT column_one.setText(stationData.COLUMN_HEADINGS[0]);//设置表头 column_one.setAlignment(SWT.LEFT);//对齐方式SWT.LEFT column.setWidth(10);//宽度 */ // 设置标题的提供者 tableViewer.setLabelProvider(new TableLabelProvider()); // 设置表格视图的内容提供者 tableViewer.setContentProvider(new TableContentProvider()); // 设置列的属性. tableViewer.setColumnProperties(stationData.columnHeads); // 定义每一列的别名 tableViewer.setColumnProperties(new String[] {"name", "down", "up", "map"}); // 设置每一列的单元格编辑组件CellEditor CellEditor[] celleditors = new CellEditor[5]; // 文本编辑框 celleditors[0] = null; celleditors[1] = new TextCellEditor(table); celleditors[2] = new TextCellEditor(table); // CheckboxCellEditor(table) 复选框 celleditors[3] = new ComboBoxCellEditor(table, StationData.MAPS, SWT.READ_ONLY); // 下拉框 Text text = (Text) celleditors[1].getControl(); // 设置第down列只能输入数值 text.addVerifyListener( new VerifyListener() { public void verifyText(VerifyEvent e) { // 输入控制键,输入中文,输入字符,输入数字 正整数验证 Pattern pattern = Pattern.compile("[0-9]\\d*"); // 正则表达式 Matcher matcher = pattern.matcher(e.text); if (matcher.matches()) // 处理数字 { /*if(Integer.parseInt(e.text) != 0)//确保输入的数字不是0 e.doit = true; else e.doit = false; */ e.doit = true; } else if (e.text.length() > 0) // 字符: 包含中文、空格 e.doit = false; else // 控制键 e.doit = true; } }); Text text1 = (Text) celleditors[2].getControl(); // 设置第up列只能输入数值 text1.addVerifyListener( new VerifyListener() { public void verifyText(VerifyEvent e) { String inStr = e.text; if (inStr.length() > 0) { e.doit = NumberUtils.isDigits(inStr); } } }); table.addMouseMoveListener( new MouseMoveListener() { public void mouseMove(MouseEvent e) { if (StationData.reloadFlag) { getStationInfo(); openCurrentTable(row); StationData.reloadFlag = false; // System.out.println("entry"); } } }); /*table.addFocusListener(new FocusListener(){ public void focusGained(FocusEvent e) { getStationInfo(); openCurrentTable(row); } @Override public void focusLost(FocusEvent e) { } }); */ tableViewer.setCellEditors(celleditors); // 设置单元的更改器 tableViewer.setCellModifier(new TableCellModifier()); tableViewer.addFilter(new TableViewerFilter()); // 过滤器 // 构造工具条 Composite buttonComposite = new Composite(group, SWT.NONE); buttonComposite.setLayoutData(new GridData(SWT.CENTER, SWT.CENTER, false, false)); // 使工具条居中 Action actionModify = new Action("更新") { public void run() { // 取得用户所选择的第一行, 若没有选择则为null IStructuredSelection selection = (IStructuredSelection) tableViewer.getSelection(); // 获取选中的第一行数据 Station station = (Station) selection.getFirstElement(); if (station != null) { if (updateStationInfo( station.getStation_downnumber(), station.getStation_upnumber(), station.getStation_graph(), station.getStation_name())) { showMsg("成功更新!"); // 表格的刷新方法,界面会重新读取数据并显示 pushCommand(); tableViewer.refresh(); // false } else { showMsg("更新失败!"); // tableViewer.refresh();//false } } else { showMsg("请选取进行更新的行!"); } } }; Action actionDelete = new Action("删除") { public void run() { // 取得用户所选择的第一行, 若没有选择则为null IStructuredSelection selection = (IStructuredSelection) tableViewer.getSelection(); // 获取选中的第一个数据 Station station = (Station) selection.getFirstElement(); if (station != null) { // 先预先移动到下一行 Table table = tableViewer.getTable(); // int i = table.getSelectionIndex(); //取得当前所选行的序号,如没有则返回-1 // table.setSelection(i + 1); //当前选择行移下一行 // 确认删除 MessageBox messageBox = new MessageBox(shell, SWT.YES | SWT.NO | SWT.ICON_INFORMATION); messageBox.setText("提示信息"); messageBox.setMessage("确定要删除此记录吗?"); // SWT.YES 是 // SWT.NO 否 // SWT.CANCEL 取消 // SWT.RETRY 重试// SWT.ABORT 放弃// SWT.IGNORE // 忽略 if (messageBox.open() == SWT.YES) { if (deleteStationInfo(station.getStation_name())) // 从数据库中删除记录 { // showMsg("成功删除!"); ((List) tableViewer.getInput()).remove(station); // 数据模型的List容器中删除 stationData.remove(station.getStation_name()); openCurrentTable(row); tableViewer.remove(station); // 从表格界面上删除 pushCommand(); } else showMsg("删除失败!"); } } else { showMsg("请选取要删除的纪录!"); } } }; Action actionClear = new Action("清空") { // 清除所显示内容,点击保存后更新库 public void run() { if (clearStationInfo()) { showMsg("清空操作成功!"); stationData.removeAll(); openCurrentTable(row); pushCommand(); } else showMsg("清空操作失败!"); } }; Action actionHelp = new Action("帮助") { public void run() { String str = "更新:\n\r" + "先对某行内容进行修改,然后点击更新进行保存\n\r" + "清空:\n\r" + "从库中物理删除所有记录"; showMsg(str); } }; Action nextPage = new Action("下一页") { public void run() { row++; if (row > stationData.getTotalPageNum()) { row--; return; } openCurrentTable(row); } }; Action prevPage = new Action("上一页") { public void run() { row--; if (row < 1) { row++; return; } openCurrentTable(row); } }; Action refresh = new Action("刷新") { public void run() { getStationInfo(); openCurrentTable(row); } }; // 工具条 ToolBar toolBar = new ToolBar(buttonComposite, SWT.FLAT | SWT.RIGHT); // |SWT.BORDER // 工具条管理器 ToolBarManager manager = new ToolBarManager(toolBar); // manager.add(refresh); // manager.add(new Separator()); manager.add(nextPage); manager.add(prevPage); manager.add(new Separator()); manager.add(actionModify); manager.add(actionDelete); manager.add(actionClear); manager.add(new Separator()); manager.add(actionHelp); manager.update(true); // 选中某行时,改变行的颜色 table.addListener( SWT.EraseItem, new Listener() { public void handleEvent(Event event) { event.detail &= ~SWT.HOT; if ((event.detail & SWT.SELECTED) == 0) return; int clientWidth = table.getClientArea().width; GC gc = event.gc; Color oldForeground = gc.getForeground(); Color oldBackground = gc.getBackground(); gc.setForeground(red); // gc.setBackground(yellow); gc.fillGradientRectangle(0, event.y, clientWidth, event.height, false); gc.setForeground(oldForeground); gc.setBackground(oldBackground); event.detail &= ~SWT.SELECTED; } }); // 在tableviewer内部为数据记录和tableItem之间的映射创建一个hash表,这样可以加快tableItem的和记录间的查找速度 // 必须保证存在要显示的数据,否则程序出错。所以这里不能用下面的语句 // tableViewer.setUseHashlookup(true);//必须在setInput之前加入才有效 // 从服务器获取数据 getStationInfo(); // 通过setInput为table添加了一个list后,只要对这个list里的元素进行添加和删除, // table中的数据就会自动添加和删除,当然每次操作后需要调用refresh方法对tableviewer进行刷新。 // 打开界面所显示的内容 tableViewer.setInput(stationData.getData()); // 自动输入数据 即将数据显示在表格中 tableViewer.setItemCount(stationData.PAGE_SIZE); // 设置显示的Item数 } //// createComp