Women in Technology

Hear us Roar



Article:
  Giving the Simple Text Editor 'Legs'
Subject:   Java Version - Solution
Date:   2002-05-25 15:12:43
From:   jsumnertx
I've been doing these examples in Java rather than Objective C. Here's the Java code version of this example


import com.apple.cocoa.application.*;
import com.apple.cocoa.foundation.*;


public class MyDocument extends NSDocument {
NSTextView textView;
NSData fileData;

public MyDocument() {
super();
}


public MyDocument(String fileName, String fileType) {
super(fileName, fileType);
}


public String windowNibName() {
return "MyDocument";
}


public void windowControllerDidLoadNib(NSWindowController aController) {
super.windowControllerDidLoadNib(aController);
// Add any code here that need to be executed once the windowController has loaded the document's window.
if ( fileData != null ) {
NSRange range=new NSRange(0,0);
textView.replaceCharactersInRangeWithRTFD(range, fileData);
}
}


public NSData dataRepresentationOfType(String aType) {
NSRange range = new NSRange(0, textView.textStorage().length());
return textView.RTFDFromRange(range);
}


public boolean loadDataRepresentation(NSData data, String aType) {
// Insert code here to read your document from the given data.
fileData=data;
return fileData!=null;
}
}

Full Threads Oldest First

Showing messages 1 through 1 of 1.

  • Java Cocoa Odysee
    2004-03-02 05:01:31  linus4200 [View]

    Hello ,

    after days of searching for a solution, I found this cocoa java code which guide me to the idea, that you might help me.

    I made a simple e-mail interface with interface builder with
    4 fields, (from, to, subject,message), whereby from, to, subject, are NSTextFields and I used the NSTextView to be able to write a text inside. On the other hand I have a java code using javamail to send an email, also very simple and one send button.

    Now my question how can I pass the NSTextView TEXT CONTENTS into the java code variable message?

    Inside the java code the variable message holds the "email message that you send" but I do not know how to fill said variable with the contents of NSTextView. If you have any
    suggestion for me it would be great. In any case thank you
    for your help,

    thx,

    linus

    the code I produce:

    /* SendMail
    e-mail function through JavaMail 1.3 from Sun
    */

    import com.apple.cocoa.foundation.*;
    import com.apple.cocoa.application.*;
    import java.util.*;
    import javax.mail.*;
    import javax.mail.internet.*;

    public class SendMail {

    public NSTextField from; /* IBOutlet */
    public NSTextView message; /* IBOutlet */
    public NSTextField subject; /* IBOutlet */
    public NSTextField to; /* IBOutlet */

    public void send(Object sender) throws Exception { /* IBAction */

    boolean debug = false;


    // defining email variables
    String host = "localhost";
    String From = from.stringValue();
    String To = to.stringValue();
    String Subject = subject.stringValue();

    // PROBLEM HOW TO USE NSTextView contents to pass it to message

    // Get system properties
    Properties props = System.getProperties();

    // Setup mail server

    props.put ("mail.smtp.host", "localhost");


    // get session
    Session session = Session.getDefaultInstance(props,null);
    session.setDebug(debug);

    // Define message
    MimeMessage messageX= new MimeMessage(session);
    messageX.setFrom(new InternetAddress(From));
    messageX.addRecipient(Message.RecipientType.TO, new InternetAddress(To));
    messageX.setSubject(Subject);
    messageX.setText(MessageY);

    // send message

    Transport.send(messageX);

    }

    }