123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246 |
- /*
- * Copyright (c) 2006-2014 DMDirc Developers
- *
- * Permission is hereby granted, free of charge, to any person obtaining a copy
- * of this software and associated documentation files (the "Software"), to deal
- * in the Software without restriction, including without limitation the rights
- * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- * copies of the Software, and to permit persons to whom the Software is
- * furnished to do so, subject to the following conditions:
- *
- * The above copyright notice and this permission notice shall be included in
- * all copies or substantial portions of the Software.
- *
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
- * SOFTWARE.
- */
-
- package com.dmdirc.addons.serverlistdialog;
-
- import com.dmdirc.actions.wrappers.PerformType;
- import com.dmdirc.actions.wrappers.PerformWrapper.PerformDescription;
- import com.dmdirc.addons.serverlists.ServerEntry;
- import com.dmdirc.addons.serverlists.ServerGroup;
- import com.dmdirc.addons.serverlists.ServerGroupItem;
- import com.dmdirc.addons.serverlists.ServerList;
- import com.dmdirc.interfaces.ConnectionManager;
- import com.dmdirc.interfaces.config.IdentityController;
- import com.dmdirc.util.collections.ListenerList;
-
- import java.net.URI;
- import java.util.List;
-
- import javax.inject.Inject;
- import javax.swing.tree.DefaultMutableTreeNode;
- import javax.swing.tree.DefaultTreeModel;
-
- /**
- * Model proxying requests from the core server list model to the swing ui.
- */
- public class ServerListModel {
-
- /** Server list. */
- private final ServerList list;
- /** Listener list. */
- private final ListenerList listeners;
- /** Active server item. */
- private ServerGroupItem activeItem;
- /** ServerManager that ServerEntrys use to create servers */
- private final ConnectionManager connectionManager;
- /** The controller to read/write settings with. */
- private final IdentityController identityController;
-
- /**
- * Creates a new server list model.
- *
- * @param connectionManager ServerManager currently in use.
- * @param identityController The controller to read/write settings with.
- * @param serverList The server list to use for the top-level entries.
- */
- @Inject
- public ServerListModel(
- final ConnectionManager connectionManager,
- final IdentityController identityController,
- final ServerList serverList) {
- this.connectionManager = connectionManager;
- this.identityController = identityController;
- list = serverList;
- listeners = new ListenerList();
- }
-
- /**
- * Returns a populated tree model for this server list model.
- *
- * @return Populated tree model
- */
- public DefaultTreeModel getTreeModel() {
- return populateModel(new DefaultTreeModel(
- new DefaultMutableTreeNode("All Servers")));
- }
-
- /**
- * Has this model got any groups?
- *
- * @return true iif there are server groups
- */
- public boolean hasItems() {
- return !list.getServerGroups().isEmpty();
- }
-
- /**
- * Populates a tree model for this server list model.
- *
- * @param model Un-populated tree model to populate
- *
- * @return Populated tree model
- */
- public DefaultTreeModel populateModel(final DefaultTreeModel model) {
- for (ServerGroup group : list.getServerGroups()) {
- final DefaultMutableTreeNode child = new DefaultMutableTreeNode(
- group);
- model.insertNodeInto(child, (DefaultMutableTreeNode) model
- .getRoot(), model.getChildCount(model.getRoot()));
- model.nodeStructureChanged((DefaultMutableTreeNode) model
- .getRoot());
- addGroups(model, child, group.getItems());
- }
- return model;
- }
-
- /**
- * Recursively adds groups to the specified tree model.
- *
- * @param model Tree model
- * @param parent Parent node to populate
- * @param items Items to add to parent node
- */
- private void addGroups(final DefaultTreeModel model,
- final DefaultMutableTreeNode parent,
- final List<ServerGroupItem> items) {
- for (ServerGroupItem group : items) {
- final DefaultMutableTreeNode child = new DefaultMutableTreeNode(
- group);
- model.insertNodeInto(child, parent, model.getChildCount(parent));
- if (group instanceof ServerGroup) {
- addGroups(model, child, ((ServerGroup) group).getItems());
- }
- }
- }
-
- /**
- * Adds a server list listener to be notified of changes.
- *
- * @param listener Listener to add
- */
- public void addServerListListener(final ServerListListener listener) {
- listeners.add(ServerListListener.class, listener);
- }
-
- /**
- * Sets the selected item in this model.
- *
- * @param item Newly selected item
- */
- public void setSelectedItem(final ServerGroupItem item) {
- activeItem = item;
- for (ServerListListener listener : listeners.get(
- ServerListListener.class)) {
- listener.serverGroupChanged(item);
- }
- }
-
- /**
- * Gets the perform description for the selected item.
- *
- * @return Perform description for the active sever group item
- */
- public PerformDescription getSelectedItemPerformDescription() {
- final PerformDescription perform;
- if (activeItem instanceof ServerEntry) {
- perform = new PerformDescription(PerformType.SERVER, activeItem
- .getName());
- } else if (activeItem instanceof ServerGroup
- && ((ServerGroup) activeItem).getNetwork() != null) {
- perform = new PerformDescription(PerformType.NETWORK,
- ((ServerGroup) activeItem).getNetwork());
- } else {
- perform = null;
- }
- return perform;
- }
-
- /**
- * Gets the currently selected item.
- *
- * @return Currently selected item
- */
- public ServerGroupItem getSelectedItem() {
- return activeItem;
- }
-
- /**
- * Saves the changes.
- *
- * @param save Do we need to save changes
- */
- public void dialogClosed(final boolean save) {
- for (ServerListListener listener : listeners.get(
- ServerListListener.class)) {
- listener.dialogClosed(save);
- }
- }
-
- /**
- * Adds a group to this model.
- *
- * @param parentGroup Parent group
- * @param groupName Group name (not null or empty)
- * @param networkName Network name (may be null or empty)
- */
- public void addGroup(final ServerGroup parentGroup,
- final String groupName, final String networkName) {
- final ServerGroup sg = new ServerGroup(identityController, groupName);
- if (networkName != null && !networkName.isEmpty()) {
- sg.setNetwork(networkName);
- }
- if (parentGroup == null) {
- list.addServerGroup(sg);
- } else {
- parentGroup.addItem(sg);
- }
- for (ServerListListener listener : listeners.get(ServerListListener.class)) {
- listener.serverGroupAdded(parentGroup, sg);
- }
- }
-
- /**
- * Adds a group to this model.
- *
- * @param parentGroup Parent group
- * @param entryName name (not null or empty)
- * @param url Valid URI
- */
- public void addEntry(final ServerGroup parentGroup, final String entryName,
- final URI url) {
- final ServerGroupItem sg = new ServerEntry(identityController, connectionManager,
- parentGroup, entryName, url, null);
- parentGroup.addItem(sg);
- for (ServerListListener listener : listeners.get(
- ServerListListener.class)) {
- listener.serverGroupAdded(parentGroup, sg);
- }
- }
-
- /**
- * Forces the server list to save all changes.
- */
- public void save() {
- list.save();
- }
-
- }
|