001 /* 002 * Gridarta MMORPG map editor for Crossfire, Daimonin and similar games. 003 * Copyright (C) 2000-2011 The Gridarta Developers. 004 * 005 * This program is free software; you can redistribute it and/or modify 006 * it under the terms of the GNU General Public License as published by 007 * the Free Software Foundation; either version 2 of the License, or 008 * (at your option) any later version. 009 * 010 * This program is distributed in the hope that it will be useful, 011 * but WITHOUT ANY WARRANTY; without even the implied warranty of 012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 013 * GNU General Public License for more details. 014 * 015 * You should have received a copy of the GNU General Public License along 016 * with this program; if not, write to the Free Software Foundation, Inc., 017 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 018 */ 019 020 package net.sf.gridarta.gui.dialog.bookmarks; 021 022 import java.awt.Component; 023 import java.io.File; 024 import javax.swing.Action; 025 import net.sf.gridarta.gui.map.mapview.MapView; 026 import net.sf.gridarta.gui.map.mapview.MapViewManager; 027 import net.sf.gridarta.gui.map.mapview.MapViewManagerListener; 028 import net.sf.gridarta.gui.mapimagecache.MapImageCache; 029 import net.sf.gridarta.gui.mapmenu.AbstractMapMenuPreferences; 030 import net.sf.gridarta.gui.mapmenu.MapMenu; 031 import net.sf.gridarta.gui.mapmenu.MapMenuEntryMap; 032 import net.sf.gridarta.model.archetype.Archetype; 033 import net.sf.gridarta.model.gameobject.GameObject; 034 import net.sf.gridarta.model.maparchobject.MapArchObject; 035 import net.sf.gridarta.model.mapcontrol.MapControl; 036 import net.sf.gridarta.model.mapmodel.MapModel; 037 import net.sf.gridarta.utils.ActionUtils; 038 import net.sf.japi.swing.action.ActionBuilder; 039 import net.sf.japi.swing.action.ActionBuilderFactory; 040 import net.sf.japi.swing.action.ActionMethod; 041 import org.jetbrains.annotations.NotNull; 042 import org.jetbrains.annotations.Nullable; 043 044 /** 045 * Implements actions for managing bookmarks. 046 * @author Andreas Kirschbaum 047 */ 048 public class BookmarkActions<G extends GameObject<G, A, R>, A extends MapArchObject<A>, R extends Archetype<G, A, R>> { 049 050 /** 051 * The {@link ActionBuilder} instance. 052 */ 053 @NotNull 054 private static final ActionBuilder ACTION_BUILDER = ActionBuilderFactory.getInstance().getActionBuilder("net.sf.gridarta"); 055 056 /** 057 * The {@link AbstractMapMenuPreferences} for adding new bookmarks. 058 */ 059 @NotNull 060 private final AbstractMapMenuPreferences bookmarksMapMenuPreferences; 061 062 /** 063 * The {@link MapMenu} defining the bookmarks menu entries. 064 */ 065 @NotNull 066 private final MapMenu mapMenu; 067 068 /** 069 * The {@link MapImageCache} for creating map previews. 070 */ 071 @NotNull 072 private final MapImageCache<G, A, R> mapImageCache; 073 074 /** 075 * The {@link MapViewManager} instance. 076 */ 077 @NotNull 078 private final MapViewManager<G, A, R> mapViewManager; 079 080 /** 081 * The parent component for dialogs. 082 */ 083 @NotNull 084 private final Component parentComponent; 085 086 /** 087 * The {@link Action} for "add bookmark". 088 */ 089 @NotNull 090 private final Action aAddBookmark = ActionUtils.newAction(ACTION_BUILDER, "Bookmarks", this, "addBookmark"); 091 092 /** 093 * The {@link ManageBookmarksDialog} instance. Set to <code>null</code> if 094 * not yet created. 095 */ 096 @Nullable 097 private ManageBookmarksDialog<G, A, R> manageBookmarksDialog; 098 099 /** 100 * Creates a new instance. 101 * @param bookmarksMapMenuPreferences the map menu preferences defining the 102 * bookmarks menu entries 103 * @param mapMenu the map menu definition the bookmarks menu entries 104 * @param mapViewManager the map view manager instance 105 * @param parentComponent the parent component for dialogs 106 * @param mapImageCache the map image cache for creating map previews 107 */ 108 public BookmarkActions(@NotNull final AbstractMapMenuPreferences bookmarksMapMenuPreferences, @NotNull final MapMenu mapMenu, @NotNull final MapViewManager<G, A, R> mapViewManager, @NotNull final Component parentComponent, @NotNull final MapImageCache<G, A, R> mapImageCache) { 109 this.bookmarksMapMenuPreferences = bookmarksMapMenuPreferences; 110 this.mapMenu = mapMenu; 111 this.mapViewManager = mapViewManager; 112 this.parentComponent = parentComponent; 113 this.mapImageCache = mapImageCache; 114 ActionUtils.newAction(ACTION_BUILDER, "Bookmarks", this, "manageBookmarks"); 115 116 final MapViewManagerListener<G, A, R> mapViewManagerListener = new MapViewManagerListener<G, A, R>() { 117 118 @Override 119 public void activeMapViewChanged(@Nullable final MapView<G, A, R> mapView) { 120 updateActions(); 121 } 122 123 @Override 124 public void mapViewCreated(@NotNull final MapView<G, A, R> mapView) { 125 // ignore 126 } 127 128 @Override 129 public void mapViewClosing(@NotNull final MapView<G, A, R> mapView) { 130 // ignore 131 } 132 133 }; 134 mapViewManager.addMapViewManagerListener(mapViewManagerListener); 135 136 updateActions(); 137 } 138 139 /** 140 * Action method for "addBookmark". Creates a new bookmark for the currently 141 * opened map. 142 */ 143 @ActionMethod 144 public void addBookmark() { 145 doAddBookmark(true); 146 } 147 148 /** 149 * Action method for "manage bookmarks". Opens the dialog for editing 150 * existing bookmarks. 151 */ 152 @ActionMethod 153 public void manageBookmarks() { 154 if (manageBookmarksDialog == null) { 155 manageBookmarksDialog = new ManageBookmarksDialog<G, A, R>(parentComponent, mapImageCache, mapMenu); 156 } 157 manageBookmarksDialog.showDialog(parentComponent); 158 } 159 160 /** 161 * Updates the enabled state of all actions. 162 */ 163 private void updateActions() { 164 aAddBookmark.setEnabled(doAddBookmark(false)); 165 } 166 167 /** 168 * Preforms the action "add bookmark". 169 * @param performAction whether the action should be performed 170 * @return whether the action has or can be performed 171 */ 172 private boolean doAddBookmark(final boolean performAction) { 173 final MapView<G, A, R> mapView = mapViewManager.getActiveMapView(); 174 if (mapView == null) { 175 return false; 176 } 177 178 final MapControl<G, A, R> mapControl = mapView.getMapControl(); 179 final MapModel<G, A, R> mapModel = mapControl.getMapModel(); 180 final File mapFile = mapModel.getMapFile(); 181 if (mapFile == null) { 182 return false; 183 } 184 185 if (performAction) { 186 final A mapArchObject = mapModel.getMapArchObject(); 187 final EditBookmarkDialog editBookmarkDialog = new EditBookmarkDialog(mapView.getInternalFrame(), mapArchObject.getMapName()); 188 if (editBookmarkDialog.showDialog()) { 189 final MapMenuEntryMap mapMenuEntry = new MapMenuEntryMap(mapFile, editBookmarkDialog.getDescription()); 190 bookmarksMapMenuPreferences.addEntry(mapMenuEntry); 191 } 192 } 193 194 return true; 195 } 196 197 }