Source code: com/maddyhome/idea/vim/option/Option.java
1 package com.maddyhome.idea.vim.option;
2
3 /*
4 * IdeaVim - A Vim emulator plugin for IntelliJ Idea
5 * Copyright (C) 2003 Rick Maddy
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20 */
21
22 import java.util.ArrayList;
23 import java.util.Comparator;
24 import java.util.Iterator;
25
26 /**
27 * Represents an VIM options that can be set with the :set command. Listeners can be set that are interested in knowing
28 * when the value of the option changes.
29 */
30 public abstract class Option
31 {
32 /**
33 * Create the option
34 * @param name The name of the option
35 * @param abbrev The short name
36 */
37 protected Option(String name, String abbrev)
38 {
39 this.name = name;
40 this.abbrev = abbrev;
41 }
42
43 /**
44 * Registers an option change listener. The listener will receive an OptionChangeEvent whenever the value of this
45 * option changes.
46 * @param listener The listener
47 */
48 public void addOptionChangeListener(OptionChangeListener listener)
49 {
50 listeners.add(listener);
51 }
52
53 /**
54 * Removes the listener from the list.
55 * @param listener The listener
56 */
57 public void removeOptionChangeListener(OptionChangeListener listener)
58 {
59 listeners.remove(listener);
60 }
61
62 /**
63 * The name of the option
64 * @return The option's name
65 */
66 public String getName()
67 {
68 return name;
69 }
70
71 /**
72 * The short name of the option
73 * @return The option's short name
74 */
75 public String getAbbreviation()
76 {
77 return abbrev;
78 }
79
80 /**
81 * Checks to see if the option's current value equals the default value
82 * @return True if equal to default, false if not.
83 */
84 public abstract boolean isDefault();
85
86 /**
87 * Sets the option to its default value.
88 */
89 public abstract void resetDefault();
90
91 /**
92 * Lets all listeners know that the value has changed. Subclasses are responsible for calling this when their
93 * value changes.
94 */
95 protected void fireOptionChangeEvent()
96 {
97 OptionChangeEvent event = new OptionChangeEvent(this);
98 for (Iterator iterator = listeners.iterator(); iterator.hasNext();)
99 {
100 OptionChangeListener listener = (OptionChangeListener)iterator.next();
101 listener.valueChange(event);
102 }
103 }
104
105 /**
106 * Helper method used to sort lists of options by their name
107 */
108 static class NameSorter implements Comparator
109 {
110 public int compare(Object o1, Object o2)
111 {
112 return ((Option)o1).name.compareTo(((Option)o2).name);
113 }
114 }
115
116 protected String name;
117 protected String abbrev;
118 protected ArrayList listeners = new ArrayList();
119 }