Commits

Roman Dawydkin committed 8c50075

Using JFace for tree view.

  • Participants
  • Parent commits 153cc42

Comments (0)

Files changed (6)

File src/ru/ezsk/ldapshell/AttributeRecord.java

+package ru.ezsk.ldapshell;
+
+import java.util.Arrays;
+
+import javax.naming.NamingEnumeration;
+import javax.naming.NamingException;
+import javax.naming.directory.Attribute;
+import javax.naming.directory.Attributes;
+
+public class AttributeRecord implements Comparable<AttributeRecord>
+{
+	public static AttributeRecord[] toArray(
+			ResultRecord resultRecord, Attributes attributes)
+			throws NamingException
+	{
+		final int size = attributes.size();
+		final AttributeRecord[] array = new AttributeRecord[size];
+		final NamingEnumeration<? extends Attribute> en =
+				attributes.getAll();
+		for (int i = 0; i < size; i++) {
+			array[i] = new AttributeRecord(resultRecord, en.nextElement());
+		}
+		Arrays.sort(array);
+		return array;
+	}
+
+	private final ResultRecord resultRecord;
+	private final String id;
+	private final String singleValue;
+	private final ValueRecord[] values;
+
+	public AttributeRecord(ResultRecord resultRecord, Attribute attribute)
+			throws NamingException
+	{
+		this.resultRecord = resultRecord;
+		id = attribute.getID();
+		final int size = attribute.size();
+		if (size == 0) {
+			singleValue = null;
+			values = null;
+		} else if (size == 1) {
+			singleValue = attribute.get().toString();
+			values = null;
+		} else {
+			singleValue = null;
+			values = new ValueRecord[size];
+			final NamingEnumeration<?> en = attribute.getAll();
+			for (int i = 0; i < size; i++) {
+				values[i] = new ValueRecord(this, en.next().toString());
+			}
+			if (!attribute.isOrdered()) {
+				Arrays.sort(values);
+			}
+		}
+	}
+
+	public ResultRecord getResultRecord() {
+		return resultRecord;
+	}
+
+	public String getId() {
+		return id;
+	}
+
+	public String getSingleValue() {
+		return singleValue;
+	}
+
+	public ValueRecord[] getValues() {
+		return values;
+	}
+
+	public int compareTo(AttributeRecord attributeRecord) {
+		return this.id.compareTo(attributeRecord.id);
+	}
+}

File src/ru/ezsk/ldapshell/LdapWindow.java

 import java.io.InputStream;
 import java.io.InputStreamReader;
 import java.io.Reader;
-import java.util.ArrayList;
-import java.util.Collections;
-import java.util.Comparator;
-import java.util.List;
 import java.util.Properties;
 
 import javax.naming.NamingEnumeration;
 import javax.naming.NamingException;
-import javax.naming.directory.Attribute;
 import javax.naming.directory.SearchResult;
 
 import org.eclipse.jface.action.Action;
 import org.eclipse.jface.resource.ImageDescriptor;
 import org.eclipse.jface.resource.ImageRegistry;
 import org.eclipse.jface.resource.JFaceResources;
+import org.eclipse.jface.viewers.TreeViewer;
 import org.eclipse.jface.window.ApplicationWindow;
 import org.eclipse.jface.window.Window;
 import org.eclipse.swt.SWT;
 import org.eclipse.swt.events.DisposeListener;
 import org.eclipse.swt.events.TraverseEvent;
 import org.eclipse.swt.events.TraverseListener;
-import org.eclipse.swt.graphics.Font;
 import org.eclipse.swt.graphics.Image;
 import org.eclipse.swt.layout.GridData;
 import org.eclipse.swt.widgets.Combo;
 import org.eclipse.swt.widgets.TabItem;
 import org.eclipse.swt.widgets.Tree;
 import org.eclipse.swt.widgets.TreeColumn;
-import org.eclipse.swt.widgets.TreeItem;
 
 public class LdapWindow extends ApplicationWindow
 {
 	private static final String ICON_KEY = "icon";
 
-	private static class AttributeComparator implements Comparator<Attribute>
-	{
-		static final Comparator<Attribute> INSTANCE = new AttributeComparator();
-
-		public int compare(Attribute o1, Attribute o2) {
-			return o1.getID().compareTo(o2.getID());
-		}
-	}
-
 	public static Properties loadProperties() throws IOException {
 		final Properties properties = new Properties();
 		final String configFileName = System.getProperty("user.home")
 	StyledText console;
 	Combo input;
 	Tree tree;
-	private Font boldFont;
 	TabFolder tabFolder;
+	private TreeViewer treeViewer;
+	private FontRegistry fontRegistry;
 
 	// ============================================================ Constructors
 	public LdapWindow(LdapDirectory ldapDirectory) {
 		super(null);
 		addMenuBar();
 		this.ldapDirectory = ldapDirectory;
+		fontRegistry = JFaceResources.getFontRegistry();
 	}
 
 	// ================================================================= Methods
 		});
 		shell.setSize(800, 600);
 		shell.setText("LDAP Shell");
-
-		final FontRegistry fontRegistry = JFaceResources.getFontRegistry();
-		boldFont = fontRegistry.getBold(JFaceResources.DEFAULT_FONT);
 	}
 
 	void disposed() {
 	}
 
 	private Control createTreePanel(Composite parent) {
-		tree = new Tree(parent, SWT.BORDER | SWT.V_SCROLL | SWT.FULL_SELECTION);
+		treeViewer = new TreeViewer(parent,
+				SWT.BORDER | SWT.V_SCROLL | SWT.FULL_SELECTION);
+		treeViewer.setContentProvider(new SearchContentProvider());
+		treeViewer.setLabelProvider(
+				new SearchLabelProvider(parent.getDisplay(), fontRegistry));
+
+		tree = treeViewer.getTree();
 		tree.setHeaderVisible(true);
 		tree.setLinesVisible(true);
+
 		TreeColumn column;
 		column = new TreeColumn(tree, SWT.NONE);
 		column.setText("Name");
 		column = new TreeColumn(tree, SWT.NONE);
 		column.setText("Value");
 		column.setWidth(400);
+
 		return tree;
 	}
 
 
 	void search(String query) throws NamingException {
 		printLn("> " + query);
-		tree.removeAll();
+
 		final NamingEnumeration<SearchResult> enumeration =
 				ldapDirectory.search(query);
-		while (enumeration.hasMore()) {
-			final SearchResult result = enumeration.next();
+		final ResultRecord[] records = ResultRecord.toArray(enumeration);
+		treeViewer.setInput(records);
 
-			final String itemName = result.getNameInNamespace();
-			printLn(itemName);
-			final TreeItem treeItem = new TreeItem(tree, SWT.NONE);
-			treeItem.setText(0, itemName);
-			treeItem.setFont(boldFont);
+		fillConsole(records);
+	}
 
-			final List<Attribute> list =
-					toList(result.getAttributes().getAll());
-			for (Attribute attribute : list) {
-				final String attrId = attribute.getID();
-				print("\t" + attrId + ":");
-				final TreeItem attrItem = new TreeItem(treeItem, SWT.NONE);
-				attrItem.setText(0, attrId);
-
-				final int size = attribute.size();
-				if (size <= 1) {
-					final String value = attribute.get().toString();
-					printLn(" " + value);
-					attrItem.setText(1, value);
+	private void fillConsole(ResultRecord[] records) {
+		for (ResultRecord resultRecord : records) {
+			printLn(resultRecord.getName());
+			for (AttributeRecord attributeRecord : resultRecord.getAttributes())
+			{
+				print("\t" + attributeRecord.getId() + ":");
+				final String singleValue = attributeRecord.getSingleValue();
+				if (singleValue != null) {
+					print(" ");
+					printLn(singleValue);
 				} else {
 					printLn("");
-					attrItem.setText(1, "(" + size + " values)");
-					attrItem.setForeground(1,
-							tree.getDisplay().getSystemColor(SWT.COLOR_GRAY));
-					for (int i = 0; i < size; i++) {
-						final String value = attribute.get(i).toString();
-						printLn("\t\t" + value);
-						final TreeItem valueItem =
-								new TreeItem(attrItem, SWT.NONE);
-						valueItem.setText(0, attrId);
-						valueItem.setText(1, value);
+					for (ValueRecord valueRecord: attributeRecord.getValues()) {
+						printLn("\t\t" + valueRecord.getValue());
 					}
 				}
 			}
 		console.setText("");
 		tree.removeAll();
 	}
-
-	private static List<Attribute> toList(
-			NamingEnumeration<? extends Attribute> enumeration)
-			throws NamingException
-	{
-		final ArrayList<Attribute> list = new ArrayList<>();
-		while (enumeration.hasMore()) {
-			list.add(enumeration.next());
-		}
-		Collections.sort(list, AttributeComparator.INSTANCE);
-		return list;
-	}
 }

File src/ru/ezsk/ldapshell/ResultRecord.java

+package ru.ezsk.ldapshell;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+
+import javax.naming.NamingEnumeration;
+import javax.naming.NamingException;
+import javax.naming.directory.SearchResult;
+
+public class ResultRecord implements Comparable<ResultRecord>
+{
+	public static ResultRecord[] toArray(NamingEnumeration<SearchResult> en)
+			throws NamingException
+	{
+		final ArrayList<ResultRecord> list = new ArrayList<>();
+		while (en.hasMore()) {
+			final SearchResult searchResult = en.nextElement();
+			list.add(new ResultRecord(searchResult));
+		}
+		final ResultRecord[] array = list.toArray(new ResultRecord[list.size()]);
+		Arrays.sort(array);
+		return array;
+	}
+
+	private String name;
+	private AttributeRecord[] attributes;
+
+	public ResultRecord(SearchResult result) throws NamingException {
+		name = result.getNameInNamespace();
+		attributes = AttributeRecord.toArray(this, result.getAttributes());
+	}
+
+	public String getName() {
+		return name;
+	}
+
+	public AttributeRecord[] getAttributes() {
+		return attributes;
+	}
+
+	public int compareTo(ResultRecord record) {
+		return this.name.compareTo(record.name);
+	}
+}

File src/ru/ezsk/ldapshell/SearchContentProvider.java

+package ru.ezsk.ldapshell;
+
+import org.eclipse.jface.viewers.ITreeContentProvider;
+import org.eclipse.jface.viewers.Viewer;
+
+public class SearchContentProvider implements ITreeContentProvider
+{
+	public void dispose() {
+	}
+
+	public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
+	}
+
+	public Object[] getElements(Object inputElement) {
+		return (ResultRecord[]) inputElement;
+	}
+
+	public Object[] getChildren(Object element) {
+		if (element instanceof ResultRecord) {
+			final ResultRecord result = (ResultRecord) element;
+			return result.getAttributes();
+		} else if (element instanceof AttributeRecord) {
+			final AttributeRecord attribute = (AttributeRecord) element;
+			final ValueRecord[] values = attribute.getValues();
+			return values;
+		} else {
+			return null;
+		}
+	}
+
+	public Object getParent(Object element) {
+		return null;
+	}
+
+	public boolean hasChildren(Object element) {
+		if (element instanceof ResultRecord) {
+			final ResultRecord resultRecord = (ResultRecord) element;
+			return resultRecord.getAttributes().length > 0;
+		} else if (element instanceof AttributeRecord) {
+			final AttributeRecord attributeRecord = (AttributeRecord) element;
+			final ValueRecord[] values = attributeRecord.getValues();
+			return (values != null) && (values.length > 1);
+		} else {
+			return false;
+		}
+	}
+}

File src/ru/ezsk/ldapshell/SearchLabelProvider.java

+package ru.ezsk.ldapshell;
+
+import org.eclipse.jface.resource.FontRegistry;
+import org.eclipse.jface.resource.JFaceResources;
+import org.eclipse.jface.viewers.ITableColorProvider;
+import org.eclipse.jface.viewers.ITableFontProvider;
+import org.eclipse.jface.viewers.ITableLabelProvider;
+import org.eclipse.jface.viewers.LabelProvider;
+import org.eclipse.swt.SWT;
+import org.eclipse.swt.graphics.Color;
+import org.eclipse.swt.graphics.Font;
+import org.eclipse.swt.graphics.Image;
+import org.eclipse.swt.widgets.Display;
+
+public class SearchLabelProvider extends LabelProvider
+		implements ITableLabelProvider, ITableColorProvider, ITableFontProvider
+{
+	private Color grayColor;
+	private Font boldFont;
+
+	public SearchLabelProvider(Display display, FontRegistry fontRegistry) {
+		grayColor = display.getSystemColor(SWT.COLOR_GRAY);
+		boldFont = fontRegistry.getBold(JFaceResources.DEFAULT_FONT);
+	}
+
+	public Image getColumnImage(Object element, int columnIndex) {
+		return null;
+	}
+
+	public String getColumnText(Object element, int columnIndex) {
+		if (element instanceof ResultRecord) {
+			final ResultRecord result = (ResultRecord) element;
+			switch (columnIndex) {
+				case 0:
+					return result.getName();
+				case 1:
+					return "";
+				default:
+					return null;
+			}
+		} else if (element instanceof AttributeRecord) {
+			final AttributeRecord attribute = (AttributeRecord) element;
+			switch (columnIndex) {
+				case 0:
+					return attribute.getId();
+				case 1:
+					final ValueRecord[] values = attribute.getValues();
+					if (values == null || values.length == 0) {
+						return attribute.getSingleValue();
+					} else {
+						return "(" + values.length + " values)";
+					}
+				default:
+					return null;
+			}
+		} else if (element instanceof ValueRecord) {
+			final ValueRecord value = (ValueRecord) element;
+			switch (columnIndex) {
+				case 0:
+					return value.getId();
+				case 1:
+					return value.getValue();
+				default:
+					return null;
+			}
+		} else {
+			return null;
+		}
+	}
+
+	public Font getFont(Object element, int columnIndex) {
+		if ((element instanceof ResultRecord) && (columnIndex == 0)) {
+			return boldFont;
+		} else {
+			return null;
+		}
+	}
+
+	public Color getForeground(Object element, int columnIndex) {
+		if ((element instanceof AttributeRecord) && (columnIndex == 1)) {
+			final AttributeRecord attributeRecord = (AttributeRecord) element;
+			if (attributeRecord.getSingleValue() != null) {
+				return null;
+			} else {
+				return grayColor;
+			}
+		} else {
+			return null;
+		}
+	}
+
+	public Color getBackground(Object element, int columnIndex) {
+		return null;
+	}
+}

File src/ru/ezsk/ldapshell/ValueRecord.java

+package ru.ezsk.ldapshell;
+
+public class ValueRecord implements Comparable<ValueRecord>
+{
+	private final AttributeRecord attributeRecord;
+	private final String value;
+
+	public ValueRecord(AttributeRecord attributeRecord, String value) {
+		this.attributeRecord = attributeRecord;
+		this.value = value;
+	}
+
+	public AttributeRecord getAttributeRecord() {
+		return attributeRecord;
+	}
+
+	public String getId() {
+		return attributeRecord.getId();
+	}
+
+	public String getValue() {
+		return value;
+	}
+
+	public int compareTo(ValueRecord valueRecord) {
+		return this.value.compareTo(valueRecord.value);
+	}
+}