| 1 |
module jface.FileTreeViewer; |
|---|
| 2 |
|
|---|
| 3 |
import dwt.DWT; |
|---|
| 4 |
import dwt.widgets.Label; |
|---|
| 5 |
import dwt.widgets.Control; |
|---|
| 6 |
import dwt.widgets.Composite; |
|---|
| 7 |
import dwt.widgets.Display; |
|---|
| 8 |
import dwt.widgets.Shell; |
|---|
| 9 |
import dwt.widgets.Button; |
|---|
| 10 |
|
|---|
| 11 |
import dwt.events.SelectionAdapter; |
|---|
| 12 |
import dwt.events.SelectionEvent; |
|---|
| 13 |
|
|---|
| 14 |
import dwt.graphics.Image; |
|---|
| 15 |
import dwt.graphics.ImageData; |
|---|
| 16 |
|
|---|
| 17 |
import dwt.layout.GridLayout; |
|---|
| 18 |
import dwt.layout.GridData; |
|---|
| 19 |
|
|---|
| 20 |
import dwtx.jface.window.ApplicationWindow; |
|---|
| 21 |
|
|---|
| 22 |
import dwtx.jface.viewers.Viewer; |
|---|
| 23 |
import dwtx.jface.viewers.TreeViewer; |
|---|
| 24 |
import dwtx.jface.viewers.ITreeContentProvider; |
|---|
| 25 |
import dwtx.jface.viewers.ILabelProvider; |
|---|
| 26 |
import dwtx.jface.viewers.ILabelProviderListener; |
|---|
| 27 |
import dwtx.jface.viewers.LabelProviderChangedEvent; |
|---|
| 28 |
|
|---|
| 29 |
version(JIVE) import jive.stacktrace; |
|---|
| 30 |
|
|---|
| 31 |
import dwt.dwthelper.utils; |
|---|
| 32 |
import dwt.dwthelper.ByteArrayInputStream; |
|---|
| 33 |
|
|---|
| 34 |
//------------------------------------ |
|---|
| 35 |
//import dwt.dwthelper.utils; |
|---|
| 36 |
//------------------------------------ |
|---|
| 37 |
|
|---|
| 38 |
import tango.io.FileSystem; |
|---|
| 39 |
import tango.io.Path; |
|---|
| 40 |
import tango.io.FilePath; |
|---|
| 41 |
import tango.util.log.Trace; |
|---|
| 42 |
|
|---|
| 43 |
import dwtx.dwtxhelper.Collection; |
|---|
| 44 |
import tango.text.convert.Utf; |
|---|
| 45 |
|
|---|
| 46 |
void main(){ |
|---|
| 47 |
auto hw = new FileTree; |
|---|
| 48 |
hw.run(); |
|---|
| 49 |
} |
|---|
| 50 |
|
|---|
| 51 |
class FileTree : ApplicationWindow { |
|---|
| 52 |
|
|---|
| 53 |
TreeViewer tv; |
|---|
| 54 |
|
|---|
| 55 |
this(){ |
|---|
| 56 |
super(null); |
|---|
| 57 |
} |
|---|
| 58 |
|
|---|
| 59 |
public void run(){ |
|---|
| 60 |
setBlockOnOpen(true); |
|---|
| 61 |
open(); |
|---|
| 62 |
Display.getCurrent().dispose(); |
|---|
| 63 |
} |
|---|
| 64 |
|
|---|
| 65 |
protected void configureShell( Shell shell ){ |
|---|
| 66 |
super.configureShell(shell); |
|---|
| 67 |
shell.setText( "File Tree" ); |
|---|
| 68 |
shell.setSize( 400, 400 ); |
|---|
| 69 |
} |
|---|
| 70 |
|
|---|
| 71 |
protected Control createContents(Composite parent){ |
|---|
| 72 |
|
|---|
| 73 |
auto composite = new Composite( parent, DWT.NONE ); |
|---|
| 74 |
composite.setLayout( new GridLayout(1,false)); |
|---|
| 75 |
|
|---|
| 76 |
// Add a checkbox to toggle whether the labels preserve case |
|---|
| 77 |
auto preserveCase = new Button( composite, DWT.CHECK ); |
|---|
| 78 |
preserveCase.setText( "&Preserve case" ); |
|---|
| 79 |
|
|---|
| 80 |
// Create the tree viewer to display the file tree |
|---|
| 81 |
tv = new TreeViewer( composite ); |
|---|
| 82 |
tv.getTree().setLayoutData( new GridData( GridData.FILL_BOTH )); |
|---|
| 83 |
tv.setContentProvider( new FileTreeContentProvider()); |
|---|
| 84 |
tv.setLabelProvider( new FileTreeLabelProvider() ); |
|---|
| 85 |
tv.setInput( stringcast("root") ); |
|---|
| 86 |
|
|---|
| 87 |
// When user checks the checkbox, toggle the preserve case attribute |
|---|
| 88 |
// of the label provider |
|---|
| 89 |
preserveCase.addSelectionListener( new class SelectionAdapter{ |
|---|
| 90 |
public void widgetSelected( SelectionEvent event ){ |
|---|
| 91 |
auto preserveCase = (cast(Button)event.widget).getSelection(); |
|---|
| 92 |
auto ftlp = cast(FileTreeLabelProvider) tv.getLabelProvider(); |
|---|
| 93 |
ftlp.setPreserveCase(preserveCase); |
|---|
| 94 |
} |
|---|
| 95 |
}); |
|---|
| 96 |
return composite; |
|---|
| 97 |
} |
|---|
| 98 |
} |
|---|
| 99 |
|
|---|
| 100 |
class FileTreeContentProvider : ITreeContentProvider { |
|---|
| 101 |
public override Object[] getChildren( Object arg0 ){ |
|---|
| 102 |
auto fp = cast(FilePath)arg0; |
|---|
| 103 |
try{ |
|---|
| 104 |
if( !fp.isFolder() ){ |
|---|
| 105 |
return null; |
|---|
| 106 |
} |
|---|
| 107 |
Object[] res; |
|---|
| 108 |
foreach( item; fp ){ |
|---|
| 109 |
res ~= FilePath.from( item ); |
|---|
| 110 |
} |
|---|
| 111 |
return res; |
|---|
| 112 |
} |
|---|
| 113 |
catch( Exception e ){ |
|---|
| 114 |
return null; |
|---|
| 115 |
} |
|---|
| 116 |
} |
|---|
| 117 |
|
|---|
| 118 |
public override Object getParent(Object arg0 ){ |
|---|
| 119 |
auto fp = cast(FilePath)arg0; |
|---|
| 120 |
return fp.pop; |
|---|
| 121 |
} |
|---|
| 122 |
|
|---|
| 123 |
public override bool hasChildren(Object arg0 ){ |
|---|
| 124 |
auto obj = getChildren(arg0); |
|---|
| 125 |
return obj is null ? false : obj.length > 0; |
|---|
| 126 |
} |
|---|
| 127 |
|
|---|
| 128 |
public override Object[] getElements( Object arg0 ){ |
|---|
| 129 |
Object[] res; |
|---|
| 130 |
|
|---|
| 131 |
foreach( root; FileSystem.roots()){ |
|---|
| 132 |
// ignore floppy drives, they bring up strange error messages |
|---|
| 133 |
if( root == `A:\`|| root == `B:\` ){ |
|---|
| 134 |
continue; |
|---|
| 135 |
} |
|---|
| 136 |
res ~= new FilePath( tango.io.Path.standard( root )); |
|---|
| 137 |
} |
|---|
| 138 |
return res; |
|---|
| 139 |
} |
|---|
| 140 |
|
|---|
| 141 |
public override void dispose(){ |
|---|
| 142 |
} |
|---|
| 143 |
|
|---|
| 144 |
public override void inputChanged(Viewer arg0, Object arg1, Object arg2 ){ |
|---|
| 145 |
} |
|---|
| 146 |
|
|---|
| 147 |
} |
|---|
| 148 |
|
|---|
| 149 |
class FileTreeLabelProvider : ILabelProvider { |
|---|
| 150 |
|
|---|
| 151 |
private ArrayList listeners; |
|---|
| 152 |
|
|---|
| 153 |
private Image file; |
|---|
| 154 |
private Image dir; |
|---|
| 155 |
|
|---|
| 156 |
private bool preserveCase; |
|---|
| 157 |
|
|---|
| 158 |
public this(){ |
|---|
| 159 |
listeners = new ArrayList(); |
|---|
| 160 |
|
|---|
| 161 |
file = new Image( null, new ImageData( new ByteArrayInputStream( cast(byte[])import( "file.png" )))); |
|---|
| 162 |
dir = new Image( null, new ImageData( new ByteArrayInputStream( cast(byte[])import( "folder.png" )))); |
|---|
| 163 |
} |
|---|
| 164 |
|
|---|
| 165 |
public void setPreserveCase(bool preserveCase){ |
|---|
| 166 |
this.preserveCase = preserveCase; |
|---|
| 167 |
auto event = new LabelProviderChangedEvent(this); |
|---|
| 168 |
for( int i = 0, n = listeners.size(); i < n; i++ ){ |
|---|
| 169 |
auto ilpl = cast(ILabelProviderListener)listeners.get(i); |
|---|
| 170 |
ilpl.labelProviderChanged(event); |
|---|
| 171 |
} |
|---|
| 172 |
} |
|---|
| 173 |
|
|---|
| 174 |
public override Image getImage(Object arg0){ |
|---|
| 175 |
auto fp = cast(FilePath)arg0; |
|---|
| 176 |
// is a root |
|---|
| 177 |
if( fp.name.length is 0 ){ |
|---|
| 178 |
return dir; |
|---|
| 179 |
} |
|---|
| 180 |
return fp.isFolder() ? dir : file; |
|---|
| 181 |
} |
|---|
| 182 |
|
|---|
| 183 |
public override char[] getText(Object arg0){ |
|---|
| 184 |
auto fp = cast(FilePath)arg0; |
|---|
| 185 |
auto text = fp.name(); |
|---|
| 186 |
if( text.length is 0 ){ |
|---|
| 187 |
// now take all info, it will be drive or the root folder |
|---|
| 188 |
text = fp.toString(); |
|---|
| 189 |
} |
|---|
| 190 |
return preserveCase ? text : text.toUpperCase(); |
|---|
| 191 |
} |
|---|
| 192 |
|
|---|
| 193 |
public void addListener( ILabelProviderListener arg0 ){ |
|---|
| 194 |
listeners.add(cast(Object)arg0); |
|---|
| 195 |
} |
|---|
| 196 |
|
|---|
| 197 |
public void dispose(){ |
|---|
| 198 |
if( dir !is null ) dir.dispose(); |
|---|
| 199 |
if( file !is null ) file.dispose(); |
|---|
| 200 |
} |
|---|
| 201 |
|
|---|
| 202 |
public bool isLabelProperty(Object arg0, char[] arg1){ |
|---|
| 203 |
return false; |
|---|
| 204 |
} |
|---|
| 205 |
|
|---|
| 206 |
public void removeListener(ILabelProviderListener arg0){ |
|---|
| 207 |
listeners.remove(cast(Object)arg0); |
|---|
| 208 |
} |
|---|
| 209 |
} |
|---|