changeset 114:3476fddbb4c8

addressbook compiles
author Frank Benoit <benoit@tionex.de>
date Sat, 19 Jan 2008 12:39:19 +0100
parents 1401263f71b0
children 52b32f5cb1e0
files dwtexamples/addressbook/AddressBook.d dwtexamples/addressbook/DataEntryDialog.d dwtexamples/addressbook/SearchDialog.d
diffstat 3 files changed, 96 insertions(+), 96 deletions(-) [+]
line wrap: on
line diff
--- a/dwtexamples/addressbook/AddressBook.d	Sat Jan 19 12:21:23 2008 +0100
+++ b/dwtexamples/addressbook/AddressBook.d	Sat Jan 19 12:39:19 2008 +0100
@@ -44,9 +44,26 @@
 
 import dwtexamples.addressbook.SearchDialog;
 import dwtexamples.addressbook.DataEntryDialog;
+import dwtexamples.addressbook.FindListener;
 
 import dwt.dwthelper.ResourceBundle;
 
+import tango.core.Exception;
+import tango.io.FilePath;
+import tango.io.File;
+import tango.io.FileConduit;
+import tango.io.stream.FileStream;
+import TextUtil = tango.text.Util;
+import Unicode = tango.text.Unicode;
+
+/*** Linker workaround start ***/
+import tango.io.Stdout;
+import tango.math.Math;
+import tango.text.convert.Format;
+import tango.util.Convert;
+import tango.util.PathUtil;
+/*** Linker workaround end ***/
+
 void main() {
     Display display = new Display();
     AddressBook application = new AddressBook();
@@ -72,7 +89,7 @@
     private Table table;
     private SearchDialog searchDialog;
 
-    private File file;
+    private FilePath file;
     private bool isModified;
 
     private char[][] copyBuffer;
@@ -187,24 +204,11 @@
  * Converts an encoded <code>char[]</code> to a char[] array representing a table entry.
  */
 private char[][] decodeLine(char[] line) {
-    if(line is null) return null;
-
-    char[][] parsedLine = new char[][table.getColumnCount()];
-    for(int i = 0; i < parsedLine.length - 1; i++) {
-        int index = line.indexOf(DELIMITER);
-        if (index > -1) {
-            parsedLine[i] = line.substring(0, index);
-            line = line.substring(index + DELIMITER.length(), line.length());
-        } else {
-            return null;
-        }
+    char[][] toks = TextUtil.split( line, DELIMITER );
+    while( toks.length < table.getColumnCount() ){
+        toks ~= "";
     }
-
-    if (line.indexOf(DELIMITER) !is -1) return null;
-
-    parsedLine[parsedLine.length - 1] = line;
-
-    return parsedLine;
+    return toks[ 0 .. table.getColumnCount() ];
 }
 private void displayError(char[] msg) {
     MessageBox box = new MessageBox(shell, DWT.ICON_ERROR);
@@ -228,9 +232,9 @@
 private char[] encodeLine(char[][] tableItems) {
     char[] line = "";
     for (int i = 0; i < tableItems.length - 1; i++) {
-        line += tableItems[i] + DELIMITER;
+        line ~= tableItems[i] ~ DELIMITER;
     }
-    line += tableItems[tableItems.length - 1] + "\n";
+    line ~= tableItems[tableItems.length - 1] ~ "\n";
 
     return line;
 }
@@ -243,48 +247,51 @@
     char[] searchString = searchDialog.getSearchString();
     int column = searchDialog.getSelectedSearchArea();
 
-    searchString = matchCase ? searchString : searchString.toLowerCase();
+    searchString = matchCase ? searchString : Unicode.toLower( searchString );
 
     bool found = false;
     if (searchDialog.getSearchDown()) {
         for(int i = table.getSelectionIndex() + 1; i < table.getItemCount(); i++) {
-            if (found = findMatch(searchString, table.getItem(i), column, matchWord, matchCase)){
+            found = findMatch(searchString, table.getItem(i), column, matchWord, matchCase);
+            if ( found ){
                 table.setSelection(i);
                 break;
             }
         }
     } else {
         for(int i = table.getSelectionIndex() - 1; i > -1; i--) {
-            if (found = findMatch(searchString, table.getItem(i), column, matchWord, matchCase)){
+            found = findMatch(searchString, table.getItem(i), column, matchWord, matchCase);
+            if ( found ){
                 table.setSelection(i);
                 break;
             }
         }
     }
 
-    shell.setCursor(null);
+    shell.setCursor(cast(Cursor)null);
     waitCursor.dispose();
 
     return found;
 }
 private bool findMatch(char[] searchString, TableItem item, int column, bool matchWord, bool matchCase) {
 
-    char[] tableText = matchCase ? item.getText(column) : item.getText(column).toLowerCase();
+    char[] tableText = matchCase ? item.getText(column) : Unicode.toLower( item.getText(column));
     if (matchWord) {
-        if (tableText !is null && tableText.equals(searchString)) {
+        if (tableText !is null && tableText==searchString) {
             return true;
         }
 
     } else {
-        if(tableText!is null && tableText.indexOf(searchString) !is -1) {
+        if(tableText!is null && TextUtil.containsPattern( tableText, searchString)) {
             return true;
         }
     }
     return false;
 }
 private void newAddressBook() {
-    shell.setText(resAddressBook.getString("Title_bar") + resAddressBook.getString("New_title"));
-    file = null;
+    shell.setText(resAddressBook.getString("Title_bar") ~ resAddressBook.getString("New_title"));
+    *(cast(Object*)&file) = null;
+    ///cast(Object)file = null;
     isModified = false;
 }
 private void newEntry() {
@@ -302,56 +309,40 @@
     FileDialog fileDialog = new FileDialog(shell, DWT.OPEN);
 
     fileDialog.setFilterExtensions(["*.adr;", "*.*"]);
-    fileDialog.setFilterNames(new char[][] [resAddressBook.getString("Book_filter_name") + " (*.adr)",
-                                            resAddressBook.getString("All_filter_name") + " (*.*)"]);
+    fileDialog.setFilterNames([
+        resAddressBook.getString("Book_filter_name") ~ " (*.adr)",
+        resAddressBook.getString("All_filter_name") ~ " (*.*)"]);
     char[] name = fileDialog.open();
 
     if(name is null) return;
-    File file = new File(name);
+    FilePath file = new FilePath(name);
     if (!file.exists()) {
-        displayError(resAddressBook.getString("File")~file.getName()~" "~resAddressBook.getString("Does_not_exist"));
+        displayError(resAddressBook.getString("File")~file.toString()~" "~resAddressBook.getString("Does_not_exist"));
         return;
     }
 
     Cursor waitCursor = new Cursor(shell.getDisplay(), DWT.CURSOR_WAIT);
     shell.setCursor(waitCursor);
 
-    FileReader fileReader = null;
-    BufferedReader bufferedReader = null;
-    char[][] data = new char[][0];
+    char[][] data;
     try {
-        fileReader = new FileReader(file.getAbsolutePath());
-        bufferedReader = new BufferedReader(fileReader);
-        char[] nextLine = bufferedReader.readLine();
-        while (nextLine !is null){
-            char[][] newData = new char[][data.length + 1];
-            System.arraycopy(data, 0, newData, 0, data.length);
-            newData[data.length] = nextLine;
-            data = newData;
-            nextLine = bufferedReader.readLine();
-        }
-    } catch(FileNotFoundException e) {
-        displayError(resAddressBook.getString("File_not_found") ~ "\n" ~ file.getName());
-        return;
+        scope ioFile = new File (file);
+        data = TextUtil.splitLines (cast(char[]) ioFile.read);
     } catch (IOException e ) {
-        displayError(resAddressBook.getString("IO_error_read") ~ "\n" ~ file.getName());
+        displayError(resAddressBook.getString("IO_error_read") ~ "\n" ~ file.toString());
         return;
     } finally {
 
-        shell.setCursor(null);
+        shell.setCursor(cast(Cursor)null);
         waitCursor.dispose();
-
-        if(fileReader !is null) {
-            try {
-                fileReader.close();
-            } catch(IOException e) {
-                displayError(resAddressBook.getString("IO_error_close") ~ "\n" ~file.getName());
-                return;
-            }
-        }
     }
 
-    char[][][] tableInfo = new char[][data.length][table.getColumnCount()];
+    char[][][] tableInfo = new char[][][](data.length,table.getColumnCount());
+    foreach( idx, line; data ){
+        char[][] linetoks = decodeLine(line);
+        tableInfo[ idx ] = linetoks;
+    }
+    /+
     int writeIndex = 0;
     for (int i = 0; i < data.length; i++) {
         char[][] line = decodeLine(data[i]);
@@ -362,7 +353,8 @@
         System.arraycopy(tableInfo, 0, result, 0, writeIndex);
         tableInfo = result;
     }
-    Arrays.sort(tableInfo, new RowComparator(0));
+    +/
+    tango.core.Array.sort( tableInfo, new RowComparator(0));
 
     for (int i = 0; i < tableInfo.length; i++) {
         TableItem item = new TableItem(table, DWT.NONE);
@@ -388,33 +380,38 @@
         lines[i] = encodeLine(itemText);
     }
 
-    FileWriter fileWriter = null;
+    FileOutput fileOutput;
+    bool result = true;
     try {
-        fileWriter = new FileWriter(file.getAbsolutePath(), false);
+        fileOutput = new FileOutput( file.toString );
         for (int i = 0; i < lines.length; i++) {
-            fileWriter.write(lines[i]);
+            fileOutput.write(lines[i]);
         }
-    } catch(FileNotFoundException e) {
-        displayError(resAddressBook.getString("File_not_found") ~ "\n" ~ file.getName());
-        return false;
     } catch(IOException e ) {
-        displayError(resAddressBook.getString("IO_error_write") ~ "\n" ~ file.getName());
-        return false;
+        displayError(resAddressBook.getString("IO_error_write") ~ "\n" ~ file.toString());
+        result = false;
+    } catch(TracedException e2 ) {
+        displayError(resAddressBook.getString("error_write") ~ "\n" ~ e2.toString());
+        result = false;
     } finally {
         shell.setCursor(null);
         waitCursor.dispose();
 
-        if(fileWriter !is null) {
-            try {
-                fileWriter.close();
-            } catch(IOException e) {
-                displayError(resAddressBook.getString("IO_error_close") ~ "\n" ~ file.getName());
-                return false;
-            }
+    }
+
+    if(fileOutput !is null) {
+        try {
+            fileOutput.close();
+        } catch(IOException e) {
+            displayError(resAddressBook.getString("IO_error_close") ~ "\n" ~ file.toString());
+            return false;
         }
     }
+    if( !result ){
+        return false;
+    }
 
-    shell.setText(resAddressBook.getString("Title_bar")+file.getName());
+    shell.setText(resAddressBook.getString("Title_bar")~file.toString());
     isModified = false;
     return true;
 }
@@ -427,17 +424,18 @@
     saveDialog.open();
     char[] name = saveDialog.getFileName();
 
-    if(name.equals("")) return false;
+    if(!name) return false;
 
-    if(name.indexOf(".adr") !is name.length() - 4) {
-        name += ".adr";
+    if( TextUtil.locatePatternPrior( name, ".adr" ) !is name.length - 4) {
+        name ~= ".adr";
     }
 
-    File file = new File(saveDialog.getFilterPath(), name);
+    FilePath file = new FilePath(saveDialog.getFilterPath() );
+    file.append( name );
     if(file.exists()) {
         MessageBox box = new MessageBox(shell, DWT.ICON_WARNING | DWT.YES | DWT.NO);
         box.setText(resAddressBook.getString("Save_as_title"));
-        box.setMessage(resAddressBook.getString("File") ~ file.getName()~" "~resAddressBook.getString("Query_overwrite"));
+        box.setMessage(resAddressBook.getString("File") ~ file.toString()~" "~resAddressBook.getString("Query_overwrite"));
         if(box.open() !is DWT.YES) {
             return false;
         }
@@ -449,14 +447,14 @@
     if(table.getItemCount() <= 1) return;
 
     TableItem[] items = table.getItems();
-    char[][][] data = new char[][items.length][table.getColumnCount()];
+    char[][][] data = new char[][][](items.length, table.getColumnCount());
     for(int i = 0; i < items.length; i++) {
         for(int j = 0; j < table.getColumnCount(); j++) {
             data[i][j] = items[i].getText(j);
         }
     }
 
-    Arrays.sort(data, new RowComparator(column));
+    tango.core.Array.sort(data, new RowComparator(column));
 
     if (lastSortColumn !is column) {
         table.setSortColumn(table.getColumn(column));
@@ -876,17 +874,18 @@
     subItem.addSelectionListener(new class() SelectionAdapter {
         public void widgetSelected(SelectionEvent e) {
             MessageBox box = new MessageBox(shell, DWT.NONE);
-            box.setText(resAddressBook.getString("About_1") + shell.getText());
-            box.setMessage(shell.getText() + resAddressBook.getString("About_2"));
+            box.setText(resAddressBook.getString("About_1") ~ shell.getText());
+            box.setMessage(shell.getText() ~ resAddressBook.getString("About_2"));
             box.open();
         }
     });
 }
 
+
 /**
  * To compare entries (rows) by the given column
  */
-private class RowComparator : Comparator {
+private class RowComparator /*: Comparator*/ {
     private int column;
 
     /**
@@ -905,11 +904,11 @@
      * @return negative if obj1 less than obj2, positive if
      *          obj1 greater than obj2, and zero if equal.
      */
-    public int compare(Object obj1, Object obj2) {
-        char[][] row1 = cast(char[][])obj1;
-        char[][] row2 = cast(char[][])obj2;
+    public bool compare(char[][] row1, char[][] row2) {
+        return row1[column] < row2[column];
+    }
 
-        return row1[column].compareTo(row2[column]);
-    }
+    alias compare opCall;
 }
+
 }
--- a/dwtexamples/addressbook/DataEntryDialog.d	Sat Jan 19 12:21:23 2008 +0100
+++ b/dwtexamples/addressbook/DataEntryDialog.d	Sat Jan 19 12:39:19 2008 +0100
@@ -25,6 +25,7 @@
 import dwt.widgets.Text;
 
 import dwt.dwthelper.ResourceBundle;
+import dwt.dwthelper.Integer;
 
 /**
  * DataEntryDialog class uses <code>org.eclipse.swt</code>
@@ -49,8 +50,8 @@
 }
 
 private void addTextListener(Text text) {
-    text.addModifyListener(new class(test) ModifyListener {
-        Test text;
+    text.addModifyListener(new class(text) ModifyListener {
+        Text text;
         this( Text text ){ this.text = text; }
         public void modifyText(ModifyEvent e){
             Integer index = cast(Integer)(this.text.getData("index"));
--- a/dwtexamples/addressbook/SearchDialog.d	Sat Jan 19 12:21:23 2008 +0100
+++ b/dwtexamples/addressbook/SearchDialog.d	Sat Jan 19 12:39:19 2008 +0100
@@ -140,7 +140,7 @@
             if (!findHandler.find()){
                 MessageBox box = new MessageBox(shell, DWT.ICON_INFORMATION | DWT.OK | DWT.PRIMARY_MODAL);
                 box.setText(shell.getText());
-                box.setMessage(resAddressBook.getString("Cannot_find") + "\"" + searchText.getText() + "\"");
+                box.setMessage(resAddressBook.getString("Cannot_find") ~ "\"" ~ searchText.getText() ~ "\"");
                 box.open();
             }
         }