001: /*******************************************************************************
002: * Copyright (c) 2000, 2007 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.pde.internal.ui.parts;
011:
012: import org.eclipse.swt.SWT;
013: import org.eclipse.swt.events.SelectionEvent;
014: import org.eclipse.swt.events.SelectionListener;
015: import org.eclipse.swt.layout.GridData;
016: import org.eclipse.swt.layout.GridLayout;
017: import org.eclipse.swt.widgets.Button;
018: import org.eclipse.swt.widgets.Composite;
019: import org.eclipse.ui.forms.widgets.FormToolkit;
020:
021: public abstract class SharedPartWithButtons extends SharedPart {
022: private String[] fButtonLabels;
023: private Button[] fButtons;
024: protected Composite fButtonContainer;
025:
026: private class SelectionHandler implements SelectionListener {
027: public void widgetSelected(SelectionEvent e) {
028: buttonSelected(e);
029: }
030:
031: public void widgetDefaultSelected(SelectionEvent e) {
032: buttonSelected(e);
033: }
034:
035: private void buttonSelected(SelectionEvent e) {
036: Integer index = (Integer) e.widget.getData();
037: SharedPartWithButtons.this .buttonSelected(
038: (Button) e.widget, index.intValue());
039: }
040: }
041:
042: public SharedPartWithButtons(String[] buttonLabels) {
043: fButtonLabels = buttonLabels;
044: }
045:
046: public void setButtonEnabled(int index, boolean enabled) {
047: if (fButtons != null && index >= 0 && fButtons.length > index) {
048: fButtons[index].setEnabled(enabled);
049: }
050: }
051:
052: protected abstract void createMainControl(Composite parent,
053: int style, int span, FormToolkit toolkit);
054:
055: protected abstract void buttonSelected(Button button, int index);
056:
057: /*
058: * @see SharedPart#createControl(Composite, FormWidgetFactory)
059: */
060: public void createControl(Composite parent, int style, int span,
061: FormToolkit toolkit) {
062: createMainLabel(parent, span, toolkit);
063: createMainControl(parent, style, span - 1, toolkit);
064: createButtons(parent, toolkit);
065: }
066:
067: protected void createButtons(Composite parent, FormToolkit toolkit) {
068: if (fButtonLabels != null && fButtonLabels.length > 0) {
069: fButtonContainer = createComposite(parent, toolkit);
070: GridData gd = new GridData(GridData.FILL_VERTICAL);
071: fButtonContainer.setLayoutData(gd);
072: fButtonContainer.setLayout(createButtonsLayout());
073: fButtons = new Button[fButtonLabels.length];
074: SelectionHandler listener = new SelectionHandler();
075: for (int i = 0; i < fButtonLabels.length; i++) {
076: String label = fButtonLabels[i];
077: if (label != null) {
078: Button button = createButton(fButtonContainer,
079: label, i, toolkit);
080: button.addSelectionListener(listener);
081: fButtons[i] = button;
082: } else {
083: createEmptySpace(fButtonContainer, 1, toolkit);
084: }
085: }
086: }
087: }
088:
089: protected GridLayout createButtonsLayout() {
090: GridLayout layout = new GridLayout();
091: layout.marginWidth = layout.marginHeight = 0;
092: return layout;
093: }
094:
095: protected Button createButton(Composite parent, String label,
096: int index, FormToolkit toolkit) {
097: Button button;
098: if (toolkit != null)
099: button = toolkit.createButton(parent, label, SWT.PUSH);
100: else {
101: button = new Button(parent, SWT.PUSH);
102: button.setText(label);
103: }
104: GridData gd = new GridData(GridData.FILL_HORIZONTAL
105: | GridData.VERTICAL_ALIGN_BEGINNING);
106: button.setLayoutData(gd);
107: button.setData(new Integer(index));
108: return button;
109: }
110:
111: protected void updateEnabledState() {
112: for (int i = 0; i < fButtons.length; i++) {
113: fButtons[i].setEnabled(isEnabled());
114: }
115: }
116:
117: protected void createMainLabel(Composite parent, int span,
118: FormToolkit toolkit) {
119: }
120:
121: /**
122: * @param index
123: * @return
124: */
125: public Button getButton(int index) {
126: //
127: if ((fButtons == null) || (index < 0)
128: || (index >= fButtons.length)) {
129: return null;
130: }
131: //
132: return fButtons[index];
133: }
134: }
|