| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||||||
| EditCallback |
|
| 0.0;0 |
| 1 | /* |
|
| 2 | * Created on Feb 27, 2005 |
|
| 3 | * |
|
| 4 | * Copyright 2004 Chris Nelson |
|
| 5 | * |
|
| 6 | * Licensed under the Apache License, Version 2.0 (the "License"); |
|
| 7 | * you may not use this file except in compliance with the License. |
|
| 8 | * You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 |
|
| 9 | * Unless required by applicable law or agreed to in writing, |
|
| 10 | * software distributed under the License is distributed on an "AS IS" BASIS, |
|
| 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
| 12 | * See the License for the specific language governing permissions and limitations under the License. |
|
| 13 | */ |
|
| 14 | package org.trails.callback; |
|
| 15 | ||
| 16 | import org.apache.hivemind.ApplicationRuntimeException; |
|
| 17 | import org.apache.hivemind.util.Defense; |
|
| 18 | import org.apache.tapestry.IRequestCycle; |
|
| 19 | import org.apache.tapestry.callback.ICallback; |
|
| 20 | import org.trails.page.EditPage; |
|
| 21 | ||
| 22 | /** |
|
| 23 | * @author Chris Nelson |
|
| 24 | * <p/> |
|
| 25 | * Returns control to an EditPage |
|
| 26 | */ |
|
| 27 | public class EditCallback extends TrailsCallback |
|
| 28 | { |
|
| 29 | protected Object model; |
|
| 30 | ||
| 31 | private boolean modelNew; |
|
| 32 | ||
| 33 | /** |
|
| 34 | * |
|
| 35 | */ |
|
| 36 | public EditCallback(String pageName, Object model) |
|
| 37 | { |
|
| 38 | 48 | super(pageName); |
| 39 | 48 | this.model = model; |
| 40 | 48 | } |
| 41 | ||
| 42 | public EditCallback(String pageName, Object model, boolean modelNew) |
|
| 43 | { |
|
| 44 | 17 | this(pageName, model); |
| 45 | 17 | this.modelNew = modelNew; |
| 46 | 17 | } |
| 47 | ||
| 48 | /* (non-Javadoc) |
|
| 49 | * @see org.apache.tapestry.callback.ICallback#performCallback(org.apache.tapestry.IRequestCycle) |
|
| 50 | */ |
|
| 51 | public void performCallback(IRequestCycle cycle) |
|
| 52 | { |
|
| 53 | 5 | Defense.notNull(cycle, "cycle"); |
| 54 | try |
|
| 55 | { |
|
| 56 | 5 | EditPage editPage = (EditPage) cycle.getPage(getPageName()); |
| 57 | 5 | editPage.setModel(model); |
| 58 | 5 | cycle.activate(editPage); |
| 59 | } |
|
| 60 | 0 | catch (ClassCastException ex) |
| 61 | { |
|
| 62 | 0 | throw new ApplicationRuntimeException(ex); |
| 63 | } |
|
| 64 | 5 | } |
| 65 | ||
| 66 | public Object getModel() |
|
| 67 | { |
|
| 68 | 9 | return model; |
| 69 | } |
|
| 70 | ||
| 71 | public void setModel(Object model) |
|
| 72 | { |
|
| 73 | 0 | this.model = model; |
| 74 | 0 | } |
| 75 | ||
| 76 | public boolean isModelNew() |
|
| 77 | { |
|
| 78 | 12 | return modelNew; |
| 79 | } |
|
| 80 | ||
| 81 | public void setModelNew(boolean modelNew) |
|
| 82 | { |
|
| 83 | 1 | this.modelNew = modelNew; |
| 84 | 1 | } |
| 85 | ||
| 86 | /** |
|
| 87 | * We should always replace this callback if its model is new. |
|
| 88 | * This works to make sure that after a model is saved for the first |
|
| 89 | * time its call back is replaced and we can go back to the right one. |
|
| 90 | */ |
|
| 91 | @Override |
|
| 92 | public boolean shouldReplace(ICallback callback) |
|
| 93 | { |
|
| 94 | 27 | if (callback instanceof EditCallback) |
| 95 | { |
|
| 96 | 12 | EditCallback editCallback = (EditCallback) callback; |
| 97 | 12 | if (editCallback.isModelNew()) |
| 98 | { |
|
| 99 | 6 | return true; |
| 100 | } else |
|
| 101 | { |
|
| 102 | 6 | return this.equals(editCallback); |
| 103 | } |
|
| 104 | } else |
|
| 105 | { |
|
| 106 | 15 | return false; |
| 107 | } |
|
| 108 | } |
|
| 109 | ||
| 110 | ||
| 111 | } |