Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Standard Widget Toolkit (SWT) » Problem to show a CheckboxTreeViewer on TabFolder
Problem to show a CheckboxTreeViewer on TabFolder [message #792835] Tue, 07 February 2012 08:24
Mikael Petterson is currently offline Mikael Petterson
Messages: 151
Registered: July 2009
Senior Member
Hi,

I have three TabItems in a TabFolder ( included in a WizardPage).
Then for one TabItem I have a CheckboxTreeViewer but it does not show up as I select tab 2.
I know that I have 2 resources in the resourceList.

Most likely I have missed something obvious but cannot see it. Can you ?
Any help is greatly appreciated.

br,

//mike

package net.sourceforge.eclipseccase.ui.wizards;

import org.eclipse.jface.viewers.CheckStateChangedEvent;

import org.eclipse.jface.viewers.ICheckStateListener;

import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import net.sourceforge.clearcase.ClearCase;
import net.sourceforge.eclipseccase.ClearCasePlugin;
import net.sourceforge.eclipseccase.ClearCaseProvider;
import net.sourceforge.eclipseccase.ui.ResourceComparator;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Path;
import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.jface.dialogs.IDialogPage;
import org.eclipse.jface.viewers.CheckboxTreeViewer;
import org.eclipse.jface.window.Window;
import org.eclipse.jface.wizard.WizardPage;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.*;
import org.eclipse.swt.layout.*;
import org.eclipse.swt.widgets.*;
import org.eclipse.ui.dialogs.ContainerSelectionDialog;

/**
 * The "New" wizard page allows setting the container for the new file as well
 * as the file name. The page will only accept file name without the extension
 * OR with the extension that matches the expected one (mpe).
 */

public class CheckinWizardPage extends WizardPage {

	private Text containerText;

	private Text commentText;

	private String comment;

	String[] comments = new String[0];

	private Text fileText;

	private static final int WIDTH_HINT = 350;

	private static final int HEIGHT_HINT = 150;

	private IResource[] resources;

	private Combo previousCommentsCombo;

	private CheckboxTreeViewer checkedTreeViewer;

	private ArrayList<IResource> resourceList;

	/**
	 * Constructor for SampleNewWizardPage.
	 * 
	 * @param pageName
	 */
	public CheckinWizardPage(IResource[] resources) {
		super("wizardPage");
		setTitle("Checkin");
		setDescription("No useful title here yet!");
		this.resources = resources;
		// sort and add to internal holder.
		Arrays.sort(resources, new ResourceComparator());
		resourceList = new ArrayList<IResource>();
		for (int i = 0; i < resources.length; i++) {
			IResource resource = resources[i];
			resourceList.add(resource);
		}
	}

	/**
	 * @see IDialogPage#createControl(Composite)
	 */
	public void createControl(Composite parent) {
		Composite mainComposite = new Composite(parent, SWT.NONE);
		setControl(mainComposite);

		TabFolder tabFolder = new TabFolder(mainComposite, SWT.NONE);
		tabFolder.setBounds(0, 20, 564, 262);

		// Tab 1 START
		TabItem tab1 = new TabItem(tabFolder, SWT.NONE);
		tab1.setText("Comments");
		Group group = new Group(tabFolder, SWT.BORDER);
		group.setLayout(new GridLayout());
		group.setLayoutData(new GridData(GridData.GRAB_HORIZONTAL | GridData.HORIZONTAL_ALIGN_FILL));
		group.setText("Checkin Comments");



		final Label commentLabel = new Label(group, SWT.NULL);
		commentLabel.setText("Edit the &comment:");

		commentText = new Text(group, SWT.BORDER | SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL);
		GridData data = new GridData(GridData.FILL_BOTH);
		data.widthHint = WIDTH_HINT;
		data.heightHint = HEIGHT_HINT;
		commentText.setLayoutData(data);
		commentText.selectAll();
		// Tabbing?
		commentText.addTraverseListener(new TraverseListener() {

			public void keyTraversed(TraverseEvent e) {
				if (e.detail == SWT.TRAVERSE_RETURN && (e.stateMask & SWT.CTRL) != 0) {
					e.doit = false;
					// CommentDialogArea.this.signalCtrlEnter();
				}
			}
		});

		// Combo for comments
		final Label prevCommentLabel = new Label(group, SWT.NULL);
		prevCommentLabel.setText("Choose a &previously entered comment:");

		previousCommentsCombo = new Combo(group, SWT.READ_ONLY);
		GridData data2 = new GridData(GridData.FILL_HORIZONTAL);
		data2.widthHint = IDialogConstants.ENTRY_FIELD_WIDTH;
		previousCommentsCombo.setLayoutData(data2);
		initializeValues();
		previousCommentsCombo.addSelectionListener(new SelectionAdapter() {

			@Override
			public void widgetSelected(SelectionEvent e) {
				int index = previousCommentsCombo.getSelectionIndex();
				if (index != -1) {
					commentText.setText(comments[index]);
				}
			}
		});

		tab1.setControl(group);

		// Tab 1 END.

		// Tab 2 START
		TabItem tab2 = new TabItem(tabFolder, SWT.NONE);
		tab2.setText("Changes");

		Composite composite = new Composite(tabFolder, SWT.NONE);
		composite.setLayout(new GridLayout());
		composite.setLayoutData(new GridData(GridData.GRAB_HORIZONTAL | GridData.HORIZONTAL_ALIGN_FILL));
		checkedTreeViewer = new CheckboxTreeViewer(composite, SWT.MULTI);
		
		checkedTreeViewer.getTree().setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
		checkedTreeViewer.setContentProvider(new ElementSelectionContentProvider());
		checkedTreeViewer.setLabelProvider(new ElementSelectionLabelProvider());
		checkedTreeViewer.setInput(resourceList);
		checkedTreeViewer.addCheckStateListener(new ICheckStateListener() {
			public void checkStateChanged(CheckStateChangedEvent event) {

				// If the item is checked . . .
				if (event.getChecked()) {

					// . . . check all its children
					checkedTreeViewer.setSubtreeChecked(event.getElement(), true);
				}

			}
		});
		checkedTreeViewer.expandAll();
		tab2.setControl(composite);
		
		// Tab 2 END

		// Tab 3
		TabItem tab3 = new TabItem(tabFolder, SWT.NONE);
		tab3.setText("Difference");

	}

	private void initializeValues() {

		// populate the previous comment list
		for (int i = 0; i < comments.length; i++) {
			previousCommentsCombo.add(flattenText(comments[i]));
		}

		// We don't want to have an initial selection
		// (see bug 32078: http://bugs.eclipse.org/bugs/show_bug.cgi?id=32078)
		previousCommentsCombo.setText(""); //$NON-NLS-1$
	}

	public IResource[] getSelectedResources() {
		ArrayList<IResource> selected = new ArrayList<IResource>();
		Object[] checkedResources = checkedTreeViewer.getCheckedElements();
		for (int i = 0; i < checkedResources.length; i++) {
			if (resourceList.contains(checkedResources[i])) {
				selected.add((IResource) checkedResources[i]);
			}
		}
		IResource[] selectedResources = new IResource[selected.size()];
		selected.toArray(selectedResources);
		return selectedResources;
	}

	/**
	 * Uses the standard container selection dialog to choose the new value for
	 * the container field.
	 */

	private void handleBrowse() {
		ContainerSelectionDialog dialog = new ContainerSelectionDialog(getShell(), ResourcesPlugin.getWorkspace().getRoot(), false, "Select new file container");
		if (dialog.open() == Window.OK) {
			Object[] result = dialog.getResult();
			if (result.length == 1) {
				containerText.setText(((Path) result[0]).toString());
			}
		}
	}

	/**
	 * Ensures that both text fields are set.
	 */

	private void dialogChanged() {
		IResource container = ResourcesPlugin.getWorkspace().getRoot().findMember(new Path(getContainerName()));
		String fileName = getFileName();

		if (getContainerName().length() == 0) {
			updateStatus("File container must be specified");
			return;
		}
		if (container == null || (container.getType() & (IResource.PROJECT | IResource.FOLDER)) == 0) {
			updateStatus("File container must exist");
			return;
		}
		if (!container.isAccessible()) {
			updateStatus("Project must be writable");
			return;
		}
		if (fileName.length() == 0) {
			updateStatus("File name must be specified");
			return;
		}
		if (fileName.replace('\\', '/').indexOf('/', 1) > 0) {
			updateStatus("File name must be valid");
			return;
		}
		int dotLoc = fileName.lastIndexOf('.');
		if (dotLoc != -1) {
			String ext = fileName.substring(dotLoc + 1);
			if (ext.equalsIgnoreCase("mpe") == false) {
				updateStatus("File extension must be \"mpe\"");
				return;
			}
		}
		updateStatus(null);
	}

	private void updateStatus(String message) {
		setErrorMessage(message);
		setPageComplete(message == null);
	}

	public String getContainerName() {
		return containerText.getText();
	}

	public String getFileName() {
		return fileText.getText();
	}

	/**
	 * Method retrieves the check-out comment for the last modified resource
	 * outside the eclipse workspace.
	 * 
	 * @param resources
	 * @return comment
	 */
	private String getLastExtCoComment(IResource[] resources) {
		long lastModificationTime = 0L;
		IResource lastModifiedResource = null;
		StringBuffer comment = new StringBuffer();
		String lastComment = "";
		for (IResource iResource : resources) {
			String path = iResource.getLocation().toOSString();
			File file = new File(path);
			long modificationTime = file.lastModified();
			if (modificationTime == 0L) {
				ClearCasePlugin.log(IStatus.WARNING, "Could not access resource," + iResource.getName(), null);
			}
			if (modificationTime > lastModificationTime) {
				lastModificationTime = modificationTime;
				lastModifiedResource = iResource;
			}

		}

		// get comment for last modified resource.
		ClearCaseProvider provider = ClearCaseProvider.getClearCaseProvider(lastModifiedResource);
		if (provider != null) {
			String element = lastModifiedResource.getLocation().toOSString();
			String[] output = provider.describe(element, ClearCase.FORMAT, "%c");
			if (output.length > 0) {
				for (int i = 0; i < output.length; i++) {
					comment.append(output[i] + "\n");
				}

			}
			lastComment = comment.toString();
		}
		return lastComment;
	}

	/**
	 * Flatten the text in the multiline comment
	 * 
	 * @param string
	 * @return String
	 */
	String flattenText(String string) {
		StringBuffer buffer = new StringBuffer(string.length() + 20);
		boolean skipAdjacentLineSeparator = true;
		for (int i = 0; i < string.length(); i++) {
			char c = string.charAt(i);
			if (c == '\r' || c == '\n') {
				if (!skipAdjacentLineSeparator) {
					buffer.append("/");
				}
				skipAdjacentLineSeparator = true;
			} else {
				buffer.append(c);
				skipAdjacentLineSeparator = false;
			}
		}
		return buffer.toString();
	}

	protected Composite createGrabbingComposite(Composite parent, int numColumns) {
		Composite composite = new Composite(parent, SWT.NULL);
		composite.setFont(parent.getFont());

		// GridLayout
		GridLayout layout = new GridLayout();
		layout.numColumns = numColumns;
		composite.setLayout(layout);

		// GridData
		GridData data = new GridData();
		data.verticalAlignment = GridData.FILL;
		data.horizontalAlignment = GridData.FILL;
		data.grabExcessHorizontalSpace = true;
		data.grabExcessVerticalSpace = true;
		composite.setLayoutData(data);
		return composite;
	}
}
Previous Topic:Update plan for embedded Mozilla browser?
Next Topic:SWT browser navigation slower then Internet Explorer
Goto Forum:
  


Current Time: Thu Aug 28 03:25:08 EDT 2014

Powered by FUDForum. Page generated in 0.10579 seconds