Plugins (1083)


Training and Consulting (44)



EPIC Poll

What is your favourite developer portal?

[ Results | Polls ]

Votes: 540
Comments: 0

Post new topic   Reply to topic
View previous topic Printable version Log in to check your private messages View next topic
Author Message
muskvar
Post subject: Serializing StyleRange array for storage in a database  PostPosted: Mar 24, 2008 - 11:24 PM



Joined: Mar 24, 2008
Posts: 1

hello all,

i have a styledText object from which i get a stylerange array str1,i need to serialize this array so that i can store it in a database:

here is the code:

Code:

import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.StyleRange;
import org.eclipse.swt.custom.StyledText;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.graphics.FontData;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
 
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.sql.*;
import java.util.Arrays;
 
public class StyledTextSample implements Serializable
{
 
  Display display = new Display();
  Shell shell = new Shell(display);
  Connection conn;
  DataBaseClass dbobject;
 
 
  StyledText styledText;
  transient StyleRange str1[];
  Connection con = null;
  ResultSet rs;
 
  int iRowCount = 0;
  PreparedStatement pstmt = null;
  Statement stmt = null;
  String sDriver =
      "org.apache.derby.jdbc.ClientDriver";
    String sURL =
      "jdbc:derby://localhost:1527/ArrayDB;create=true;user=root;password=muskvar";
   
    private void writeObject(ObjectOutputStream out) throws IOException
       {
         out.defaultWriteObject();
       }
       
    private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException
    {
       
        in.defaultReadObject();
         
    }
 
 
  public StyledTextSample() throws IOException, ClassNotFoundException
  {
 
    
    shell.setLayout(new GridLayout());
   
    styledText = new StyledText(shell, SWT.MULTI | SWT.WRAP | SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL);
   
    styledText.setLayoutData(new GridData(GridData.FILL_BOTH));
    StyleRange styleRange1 = new StyleRange();
   
   
   
   
 
   
    styledText.setText("123456789abcdefgh");
   
   
    styleRange1.start = 0;
    styleRange1.length = 5;
    styleRange1.font=new Font(shell.getDisplay(), "Courier New", 12, SWT.BOLD);
    styleRange1.underline=true;
 
    styleRange1.foreground = shell.getDisplay().getSystemColor(SWT.COLOR_BLUE);
    styleRange1.background = shell.getDisplay().getSystemColor(SWT.COLOR_YELLOW);
    styleRange1.fontStyle = SWT.BOLD;
   
    StyleRange styleRange2 = new StyleRange();
    styleRange2.start = 6;
    styleRange2.length = 2;
    styleRange2.fontStyle = SWT.NORMAL;
    styleRange2.font=new Font(shell.getDisplay(), "Arial Black", 20, SWT.BOLD);
 
   
    styleRange2.foreground = shell.getDisplay().getSystemColor(SWT.COLOR_YELLOW);
    styleRange2.background = shell.getDisplay().getSystemColor(SWT.COLOR_BLUE);
   
   
    StyleRange styleRange3 = new StyleRange();
    styleRange3.start = 8;
    styleRange3.length = 2;
    styleRange3.fontStyle = SWT.NORMAL;
    styleRange3.font=new Font(shell.getDisplay(), "Arial Black", 20, SWT.BOLD);
    //styleRange2.foreground=new Color(display, 255,0,0);
   
    styleRange3.foreground = shell.getDisplay().getSystemColor(SWT.COLOR_YELLOW);
    styleRange3.background = shell.getDisplay().getSystemColor(SWT.COLOR_BLUE);
   
   
    StyleRange styleRange4 = new StyleRange();
    styleRange4.start = 6;
    styleRange4.length = 4;
    styleRange4.fontStyle = SWT.NORMAL;
    styleRange4.font=new Font(shell.getDisplay(), "Arial Black", 20, SWT.BOLD);
 
   
    styleRange4.foreground = shell.getDisplay().getSystemColor(SWT.COLOR_YELLOW);
    styleRange4.background = shell.getDisplay().getSystemColor(SWT.COLOR_BLUE);
   
   
   
    styledText.setStyleRange(styleRange1);
    styledText.setStyleRange(styleRange2);
    styledText.setStyleRange(styleRange3);
    styledText.setStyleRange(styleRange4);
   
   
 
   
   
    System.out.println(printStyleRanges(styledText.getStyleRanges()));
    System.out.println("---------------------------");
 
    str1=styledText.getStyleRanges();
   
    ByteArrayOutputStream os = new ByteArrayOutputStream();
    ObjectOutputStream out = new ObjectOutputStream( os );
    //writeObject( out );
    out.writeObject(str1);
    out.flush();
    byte[] blob = os.toByteArray();
 
   
    try   // Attempt to load the JDBC driver
    {     // with newInstance
        Class.forName( sDriver ).newInstance();
    }
    catch( Exception e )  // error
    {
      System.err.println(
        "Failed to load current driver.");
      return;
    } // end catch
 
    try
    {
 
      con = DriverManager.getConnection(sURL);
      stmt = con.createStatement();
    }
    catch ( Exception e)
    {
      System.err.println( "problems connecting to " +
                           sURL + ":" );
      System.err.println( e.getMessage() );
 
      if( con != null)
      {
        try { con.close(); }
        catch( Exception e2 ) {}
      }
 
      return;
    } // end catch
   
   
 
   
 
    // store 'blob' in your database
   // writeBlob( blob );
   
    // DESERIALIZING
   
    // read the serialized array
   
   
    try
    {
      stmt.executeUpdate( "DROP TABLE TeeArray" );
      System.out.println(
        "Table TeeColor was removed.");
    }
    catch ( Exception e ) { /* don't care */ }
   
    try
    {
 
// may need modification for your database
      stmt.executeUpdate( "CREATE TABLE TeeArray (" +
              "TCArray      BLOB    (100K) NOT NULL" +")" );
 
      System.out.println(
        "Table TeeColor was created.");
     
     
 
     
      pstmt = con.prepareStatement(
        "INSERT INTO TeeArray VALUES(?)");
      pstmt.setBytes(1,blob);
     
     
       
     
 
       
        pstmt.executeUpdate();
     
 
      System.out.println( iRowCount +
         " Rows inserted into TeeColor.");
 
    }
    catch ( Exception e )
    {
      System.err.println(
        "problem with SQL sent to " + sURL + ":" );
      System.   err.println( e.getMessage() );
    }
 
   
   
   
 
   
   
   
 
 
   
 
    shell.setSize(300, 120);
    shell.open();
 
 
    // Set up the event loop.
    while (!shell.isDisposed()) {
      if (!display.readAndDispatch()) {
        // If no more entries in event queue
        display.sleep();
      }
    }
 
    display.dispose();
  }
 
  private String printStyleRanges(StyleRange[] styleRanges) {
   
    if(styleRanges == null)
      return "null";
    else if(styleRanges.length == 0)
      return "[]";
   
    StringBuffer sb = new StringBuffer();
   
    for(int i=0; i<styleRanges.length; i++) {
      sb.append(styleRanges[i] + "\n");
    }
   
    return sb.toString();
  }
 
 
 
  public static void main(String[] args) throws IOException, ClassNotFoundException {
    new StyledTextSample();
  }
}


i get the following exception stacktrace:
StyleRange {0, 5, fontStyle=bold, font=Font {738856535}, foreground=Color {0, 0, 255}, background=Color {255, 255, 0}, underlined}
StyleRange {6, 4, fontStyle=normal, font=Font {67767043}, foreground=Color {255, 255, 0}, background=Color {0, 0, 255}}

Exception in thread "main" java.io.NotSerializableException: org.eclipse.swt.custom.StyleRange
at java.io.ObjectOutputStream.writeObject0(Unknown Source)
at java.io.ObjectOutputStream.writeArray(Unknown Source)
at java.io.ObjectOutputStream.writeObject0(Unknown Source)
at java.io.ObjectOutputStream.writeObject(Unknown Source)
at StyledTextSample.<init>(StyledTextSample.java:186)
at StyledTextSample.main(StyledTextSample.java:613)

please help me,

thank you.
 
 View user's profile Send private message  
Reply with quote Back to top
Display posts from previous:     
Jump to:  
All times are GMT - 6 Hours
Post new topic   Reply to topic
View previous topic Printable version Log in to check your private messages View next topic
Powered by PNphpBB2 © 2003-2004 The PNphpBB Group
Credits