Re: Adding Objects to an ArrayList (Checkboxes)
Re: Adding Objects to an ArrayList (Checkboxes)
- Subject: Re: Adding Objects to an ArrayList (Checkboxes)
- From: LD <email@hidden>
- Date: Tue, 11 Oct 2005 14:44:35 +1000
Hi there,
time to go back to basics... i.e., use objects, think about objects
and relevant messages for those objects.
So, you only need a single NSMutableArray: allServers and an a single
iterator object of type EOEnterpriseObject: aServer. Now, we've got
everthing necessary to query/update our objects.
Let's assume that each object, aServer, in the array responds to the
messages "public String name()", "public Boolean isSelected()" and
"public void setIsSelected(Boolean value)".
For the gem, to get the current list of selected servers (about 2-3
lines of readable code) see updateServerSelectionsAndProcessEmail()
action below. It's simple; easy.
Likewise, to update a server object based on checkbox we update the
object in question by sending it a message. There's no need to create
a separate list for that. That would just make things more
complicated than necessary. See "setIsServerSelected" below.
Here's the full listing to play with (it's got two submit buttons,
one to simply update selections if that's needed, and the other to
update selections and return the Mail-based component...
HTH
with regards,
--
LD
== Main.java ==
//
// Main.java: Class file for WO Component 'Main'
// Project CheckBoxes
//
// Created by ldeck on 11/10/05
//
import com.webobjects.foundation.*;
import com.webobjects.appserver.*;
import com.webobjects.eocontrol.*;
import com.webobjects.eoaccess.*;
public class Main extends WOComponent {
protected EOEditingContext _ec;
/** @TypeInfo com.webobjects.eocontrol.EOEnterpriseObject */
protected NSMutableArray allServers;
public EOEnterpriseObject aServer;
public Main( WOContext context ) {
super( context );
_ec = null;
allServers = null;
}
public EOEditingContext ec() {
if ( _ec == null ) {
_ec = new EOEditingContext();
// set shared EC to null otherwise we'll have problems
saving changes for
// items in the sharedEC. The SharedEC will pick up the
changes automatically.
_ec.setSharedEditingContext( null );
}
return _ec;
}
public void setEc( EOEditingContext newEc ) {
_ec = newEc;
}
/** @TypeInfo com.webobjects.eocontrol.EOEnterpriseObject */
public NSMutableArray allServers() {
if ( allServers == null ) {
EOQualifier qualifier;
NSArray sortOrderings;
String entityName;
EOFetchSpecification fetchSpec;
// customise these as desired
entityName = "Server";
qualifier = null;
sortOrderings = NSArray.EmptyArray;
fetchSpec = new EOFetchSpecification( entityName,
qualifier, sortOrderings );
// fetch Server objects into local EC
// useful if you'll be using them in a shared EC
// so you can update them quietly
allServers = ec().objectsWithFetchSpecification
( fetchSpec );
}
return allServers;
}
public void setAllServers( NSMutableArray newAllServers ) {
allServers = EOUtilities.localInstancesOfObjects( ec(),
newAllServers ).mutableClone();
}
public boolean isServerSelected() {
return ( ( Server )aServer ).isSelected().booleanValue();
}
public void setIsServerSelected( boolean newIsServerSelected ) {
if ( isServerSelected() != newIsServerSelected ) {
( ( Server )aServer ).setIsSelected( new Boolean
( newIsServerSelected ) );
}
}
// ==== ACTIONS ====
public WOComponent updateServerSelections() {
// checkboxes are updated for us
// nothing to do here but save changes and
// return the desired component
try {
ec().saveChanges(); // if applicable
} catch ( Exception e ) {
e.printStackTrace( ( ( NSLog.PrintStreamLogger)
NSLog.out ).printStream() );
}
return null;
}
public WOComponent updateServerSelectionsAndProcessEmail() {
EmailServerContacts nextPage;
NSArray allServers;
NSDictionary allServerSelections;
updateServerSelections();
nextPage = ( EmailServerContacts )pageWithName
( "EmailServerContacts" );
nextPage.setEc( ec() );
//
// ==== the ease of KeyValueCoding ====
// ==== assume that aServer.isSelected() returns a Boolean
object ====
//
allServers = allServers();
allServerSelections = new NSDictionary( allServers,
allServers.valueForKey( "isSelected" ) );
nextPage.setServers( allServerSelections.allKeysForObject
( Boolean.TRUE ) );
return nextPage;
}
}
== YourComponent.wod ==
CheckBoxForm: WOForm {
multipleSubmit = true;
}
CheckBoxForm_CheckBox: WOCheckBox {
checked = isServerSelected;
}
CheckBoxForm_Repetition: WORepetition {
item = aServer;
list = allServers;
}
CheckBoxForm_ResetButton: WOResetButton {
}
CheckBoxForm_SendEmailSubmitButton: WOSubmitButton {
action = updateServerSelectionsAndProcessEmail;
value = "Send Email";
}
CheckBoxForm_ServerNameString: WOString {
value = aServer.name;
}
CheckBoxForm_UpdateSubmitButton: WOSubmitButton {
action = updateServerSelections;
value = "Update Selections";
}
== YourComponent.html ==
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
<html>
<head>
<meta name="generator" content="WebObjects 5.2">
<title>Untitled</title>
</head>
<body bgcolor="#FFFFFF">
<webobject name=CheckBoxForm><webobject
name=CheckBoxForm_Repetition><webobject name=CheckBoxForm_CheckBox></
webobject><webobject name=CheckBoxForm_ServerNameString></webobject>
Enable?<br>
</webobject>
<p><webobject name=CheckBoxForm_SendEmailSubmitButton></
webobject><webobject name=CheckBoxForm_UpdateSubmitButton></
webobject><webobject name=CheckBoxForm_ResetButton></webobject></p>
</webobject>
</body>
</html>
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Webobjects-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden