001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.wicket.version.undo;
018:
019: import org.apache.wicket.Component;
020: import org.apache.wicket.markup.html.form.FormComponent;
021: import org.apache.wicket.model.CompoundPropertyModel;
022: import org.apache.wicket.model.IModel;
023: import org.apache.wicket.util.lang.Classes;
024: import org.apache.wicket.util.lang.Objects;
025: import org.slf4j.Logger;
026: import org.slf4j.LoggerFactory;
027:
028: /**
029: * A model change operation.
030: *
031: * @author Jonathan Locke
032: */
033: class ModelChange extends Change {
034: private static final long serialVersionUID = 1L;
035:
036: /** log. */
037: private static final Logger log = LoggerFactory
038: .getLogger(ModelChange.class);
039:
040: /** subject. */
041: private final Component component;
042:
043: /** original model. */
044: private IModel originalModel;
045:
046: /**
047: * Construct.
048: *
049: * @param component
050: * subject of the change
051: */
052: ModelChange(final Component component) {
053: if (component == null) {
054: throw new IllegalArgumentException(
055: "argument component must be not null");
056: }
057:
058: // Save component
059: this .component = component;
060:
061: // Get component model
062: final IModel model = component.getModel();
063:
064: // If the component has a model, it's about to change!
065: if (model != null) {
066: // Should we clone the model?
067: boolean cloneModel = true;
068:
069: // If the component is a form component
070: if (model instanceof CompoundPropertyModel) {
071: if (component instanceof FormComponent) {
072: // and it's using the same model as the form
073: if (((FormComponent) component).getForm()
074: .getModel() == model) {
075: // we don't need to clone the model, because it will
076: // be re-initialized using initModel()
077: cloneModel = false;
078: }
079: } else {
080: // If the component is using the same model as the page
081: if (component.getPage().getModel() == model) {
082: // we don't need to clone the model, because it will
083: // be re-initialized using initModel()
084: cloneModel = false;
085: }
086: }
087: }
088:
089: // Clone model?
090: if (cloneModel) {
091: model.detach();
092: originalModel = (IModel) Objects.cloneModel(model);
093: } else {
094: originalModel = model;
095: }
096: }
097:
098: if (log.isDebugEnabled()) {
099: log.debug("RECORD MODEL CHANGE: changed model of " + " ("
100: + Classes.simpleName(component.getClass()) + "@"
101: + component.hashCode() + ")");
102: }
103: }
104:
105: /**
106: * @see org.apache.wicket.version.undo.Change#undo()
107: */
108: public void undo() {
109: if (log.isDebugEnabled()) {
110: log.debug("UNDO MODEL CHANGE: setting original model "
111: + originalModel + " to " + component.getPath()
112: + "@" + component.hashCode() + ")");
113: }
114:
115: component.setModel(originalModel);
116: }
117:
118: /**
119: * @see java.lang.Object#toString()
120: */
121: public String toString() {
122: return "ModelChange[component: " + component.getPath() + "]";
123: }
124: }
|