[simal] r2242 committed - Refactoring to more readable code for saving the edited data back to t...

1 view
Skip to first unread message

si...@googlecode.com

unread,
Apr 21, 2011, 11:44:29 AM4/21/11
to simal-...@googlegroups.com
Revision: 2242
Author: sander.v...@oucs.ox.ac.uk
Date: Thu Apr 21 08:44:09 2011
Log: Refactoring to more readable code for saving the edited data back
to the project. (Issue 376)
http://code.google.com/p/simal/source/detail?r=2242

Added:

/trunk/uk.ac.osswatch.simal.web/src/main/java/uk/ac/osswatch/simal/wicket/panel/project/EditableListItemModel.java

/trunk/uk.ac.osswatch.simal.web/src/main/java/uk/ac/osswatch/simal/wicket/panel/project/EditableListView.java
Deleted:

/trunk/uk.ac.osswatch.simal.web/src/main/java/uk/ac/osswatch/simal/wicket/panel/project/GenericSetWrapper.java
Modified:

/trunk/uk.ac.osswatch.simal.web/src/main/java/uk/ac/osswatch/simal/wicket/panel/project/EditProjectPanel.java

=======================================
--- /dev/null
+++
/trunk/uk.ac.osswatch.simal.web/src/main/java/uk/ac/osswatch/simal/wicket/panel/project/EditableListItemModel.java
Thu Apr 21 08:44:09 2011
@@ -0,0 +1,59 @@
+package uk.ac.osswatch.simal.wicket.panel.project;
+
+/*
+ *
+ Copyright 2011 University of Oxford *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ *
+ */
+
+import java.util.List;
+
+import org.apache.wicket.markup.html.list.ListItemModel;
+import org.apache.wicket.markup.html.list.ListView;
+
+/**
+ * Custom version of ListItemModel<T> to allow access to the
+ * ListView's List outside the object.
+ *
+ */
+public class EditableListItemModel<T> extends ListItemModel<T>
+{
+ private static final long serialVersionUID = -8873451739437744645L;
+
+ private final ListView<T> listView;
+
+ /**
+ * Create EditableListItemModel but keep the ListView locally so
+ * it can be retreived later.
+ *
+ * @param listView
+ * The ListView
+ * @param index
+ * The index of this model
+ */
+ public EditableListItemModel(final ListView<T> listView, final int index)
+ {
+ super(listView, index);
+ this.listView = listView;
+ }
+
+ /**
+ * Return the List<T> this EditableListItemModel is part of.
+ * @return
+ */
+ public List<T> getParentList() {
+ return this.listView.getModelObject();
+ }
+}
=======================================
--- /dev/null
+++
/trunk/uk.ac.osswatch.simal.web/src/main/java/uk/ac/osswatch/simal/wicket/panel/project/EditableListView.java
Thu Apr 21 08:44:09 2011
@@ -0,0 +1,133 @@
+package uk.ac.osswatch.simal.wicket.panel.project;
+
+/*
+ *
+ Copyright 2011 University of Oxford *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ *
+ */
+
+import java.util.HashSet;
+import java.util.List;
+import java.util.Set;
+
+import org.apache.wicket.ajax.AjaxRequestTarget;
+import org.apache.wicket.ajax.markup.html.form.AjaxFallbackButton;
+import org.apache.wicket.markup.html.form.Form;
+import org.apache.wicket.markup.html.form.TextField;
+import org.apache.wicket.markup.html.list.ListItem;
+import org.apache.wicket.markup.html.list.ListView;
+import org.apache.wicket.model.IModel;
+import org.apache.wicket.model.Model;
+import org.apache.wicket.model.PropertyModel;
+
+import uk.ac.osswatch.simal.model.IProject;
+import
uk.ac.osswatch.simal.wicket.panel.project.EditProjectPanel.ReadOnlyStyleBehavior;
+
+import com.hp.hpl.jena.rdf.model.Resource;
+import com.hp.hpl.jena.tdb.TDB;
+
+public class EditableListView extends ListView<String> {
+
+ private static final long serialVersionUID = 154815894763179933L;
+
+ private IProject project;
+
+ private PropertyModel<Set<String>> projectModel;
+
+ private ReadOnlyStyleBehavior rosb;
+
+ private Form<IProject> form;
+
+ public EditableListView(String labelWicketID, List<String> editableList,
IProject project, ReadOnlyStyleBehavior rosb, Form<IProject> form) {
+ super(labelWicketID, editableList);
+
+ this.project = project;
+ this.rosb = rosb;
+ this.form = form;
+
+ this.projectModel = new PropertyModel<Set<String>>(project,
labelWicketID);
+ setReuseItems(true);
+ }
+
+ protected void populateItem(ListItem<String> item) {
+ String wrapper = (String) item.getModelObject();
+ TextField<String> setItemValue = new TextField<String>(
+ "setItemValue", item.getModel());
+ setItemValue.add(this.rosb);
+ item.add(setItemValue);
+ item.add(generateDeleteItemButton(!EditProjectPanel.NEW_ITEM
+ .equals(wrapper), item));
+ }
+
+ @Override
+ protected IModel<String> getListItemModel(
+ IModel<? extends List<String>> listViewModel, int index) {
+
+
+ return new EditableListItemModel<String>(this, index) {
+ private static final long serialVersionUID = 2563628936377112626L;
+
+ @Override
+ public void setObject(String object) {
+ super.setObject(object);
+
+ Set<String> updatedSet = new HashSet<String>(getParentList());
+ projectModel.setObject(updatedSet);
+
+ // FIXME This needs to happen somewhere else
+ TDB.sync(((Resource) project.getRepositoryResource()).getModel());
+ }
+
+ };
+ }
+
+ private AjaxFallbackButton generateDeleteItemButton(
+ boolean visibilityAllowed, ListItem<String> item) {
+ AjaxFallbackButton deleteItemButton = new AjaxFallbackDeleteItemButton(
+ "deleteItem", new Model<String>("X"), this.form, item);
+
+ deleteItemButton.setVisibilityAllowed(visibilityAllowed);
+ deleteItemButton.add(this.rosb);
+ return deleteItemButton;
+
+ }
+
+ private static class AjaxFallbackDeleteItemButton extends
AjaxFallbackButton {
+
+ private static final long serialVersionUID = -6395239712922873605L;
+
+ private ListItem<String> item;
+
+ /**
+ * @param id
+ * @param model
+ * @param form
+ */
+ public AjaxFallbackDeleteItemButton(String id, IModel<String> model,
+ Form<?> form, ListItem<String> item) {
+ super(id, model, form);
+ this.item = item;
+ }
+
+ @Override
+ protected void onSubmit(AjaxRequestTarget target, Form<?> form) {
+ item.getModel().setObject(null);
+ item.setVisible(false);
+ setVisible(false);
+ target.addComponent(form);
+ }
+ }
+
+}
=======================================
---
/trunk/uk.ac.osswatch.simal.web/src/main/java/uk/ac/osswatch/simal/wicket/panel/project/GenericSetWrapper.java
Thu Apr 14 14:39:52 2011
+++ /dev/null
@@ -1,50 +0,0 @@
-package uk.ac.osswatch.simal.wicket.panel.project;
-
-import java.io.Serializable;
-import java.util.List;
-
-/*
- * Copyright 2010 University of Oxford
- *
- * Licensed under the Apache License, Version 2.0 (the "License"); *
- * you may not use this file except in compliance with the License. *
- * You may obtain a copy of the License at *
- * *
- * http://www.apache.org/licenses/LICENSE-2.0 *
- * *
- * Unless required by applicable law or agreed to in writing, *
- * software distributed under the License is distributed on an *
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY *
- * KIND, either express or implied. See the License for the *
- * specific language governing permissions and limitations *
- * under the License. *
- */
-
-/**
- * Wrapper for items in a set so they can be edited on a form. When the
Set is a
- * property of another object it needs to be updated separately, for
example
- * when OSes are edited for an IProject.
- *
- * @param <T>
- */
-public class GenericSetWrapper<T> implements Serializable {
-
- private static final long serialVersionUID = 7273060956309086010L;
-
- private List<T> editableList;
-
- public GenericSetWrapper(List<T> editableList) {
- this.editableList = editableList;
- }
-
- public List<T> getEditableList() {
- return editableList;
- }
-
- public void setEditableList(List<T> editableList) {
- // FIXME this setter is not called because the
- // ListItemModel.setObject works directly on the List.
- this.editableList = editableList;
- }
-
-}
=======================================
---
/trunk/uk.ac.osswatch.simal.web/src/main/java/uk/ac/osswatch/simal/wicket/panel/project/EditProjectPanel.java
Fri Apr 15 10:07:34 2011
+++
/trunk/uk.ac.osswatch.simal.web/src/main/java/uk/ac/osswatch/simal/wicket/panel/project/EditProjectPanel.java
Thu Apr 21 08:44:09 2011
@@ -19,7 +19,6 @@

import java.util.ArrayList;
import java.util.Collections;
-import java.util.HashSet;
import java.util.List;
import java.util.Set;

@@ -38,7 +37,6 @@
import org.apache.wicket.markup.html.form.TextArea;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.html.list.ListItem;
-import org.apache.wicket.markup.html.list.ListItemModel;
import org.apache.wicket.markup.html.list.ListView;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.model.CompoundPropertyModel;
@@ -461,88 +459,11 @@

List<String> editableList = new ArrayList<String>(labels);
editableList.add(NEW_ITEM);
- //GenericSetWrapper<String> editableListWrapper = new
GenericSetWrapper<String>(new ArrayList<String>(editableList));
-
- final PropertyModel<Set<String>> projectModel = new
PropertyModel<Set<String>>(project, labelWicketID);
-
- ListView<String> listView = new ListView<String>(
- labelWicketID, editableList) {
- private static final long serialVersionUID = 154815894763179933L;
-
- protected void populateItem(ListItem<String> item) {
- String wrapper = (String) item.getModelObject();
- TextField<String> setItemValue = new TextField<String>(
- "setItemValue", item.getModel());
- setItemValue.add(new ReadOnlyStyleBehavior());
- item.add(setItemValue);
- item.add(generateDeleteItemButton(!NEW_ITEM
- .equals(wrapper), item));
- }
-
- @Override
- protected IModel<String> getListItemModel(
- IModel<? extends List<String>> listViewModel, int index) {
- this.getModelObject();
-
- return new ListItemModel<String>(this, index) {
- private static final long serialVersionUID =
2563628936377112626L;
-
- @Override
- public void setObject(String object) {
- super.setObject(object);
-
- Set<String> updatedSet = new HashSet<String>();
- updatedSet.add(object);
- projectModel.setObject(updatedSet);
- }
-
- };
- }
-
-
- };
- listView.setReuseItems(true);
- add(listView);
+
+ add(new EditableListView (
+ labelWicketID, editableList, project, rosb, this));
}

-
- private AjaxFallbackButton generateDeleteItemButton(
- boolean visibilityAllowed, ListItem<String> item) {
- AjaxFallbackButton deleteItemButton = new
AjaxFallbackDeleteItemButton(
- "deleteItem", new Model<String>("X"), this, item);
-
- deleteItemButton.setVisibilityAllowed(visibilityAllowed);
- deleteItemButton.add(new ReadOnlyStyleBehavior());
- return deleteItemButton;
-
- }
-
- }
-
- private static class AjaxFallbackDeleteItemButton extends
AjaxFallbackButton {
-
- private static final long serialVersionUID = -6395239712922873605L;
-
- private ListItem<String> item;
-
- /**
- * @param id
- * @param model
- * @param form
- */
- public AjaxFallbackDeleteItemButton(String id, IModel<String> model,
- Form<?> form, ListItem<String> item) {
- super(id, model, form);
- this.item = item;
- }
-
- @Override
- protected void onSubmit(AjaxRequestTarget target, Form<?> form) {
- // FIXME item.getModel().getObject().setValue(null);
- item.setVisible(false);
- setVisible(false);
- target.addComponent(form);
- }
}

public class ReadOnlyStyleBehavior extends AbstractBehavior {

Reply all
Reply to author
Forward
0 new messages