1 19 20 package org.netbeans.modules.editor; 21 22 import java.io.*; 23 import javax.swing.text.Document ; 24 import org.netbeans.editor.SettingsNames; 25 import org.netbeans.editor.ext.ExtFormatter; 26 import org.netbeans.editor.SettingsDefaults; 27 import org.openide.text.IndentEngine; 28 29 34 35 public abstract class FormatterIndentEngine extends IndentEngine { 36 37 public static final String EXPAND_TABS_PROP = "expandTabs"; 39 public static final String SPACES_PER_TAB_PROP = "spacesPerTab"; 41 static final long serialVersionUID = -3408217516931076216L; 42 43 46 private transient ExtFormatter formatter; 47 48 private String [] acceptedMimeTypes; 49 50 51 public ExtFormatter getFormatter() { 52 if (formatter == null) { 53 formatter = createFormatter(); 54 } 55 return formatter; 56 } 57 58 59 protected abstract ExtFormatter createFormatter(); 60 61 public Object getValue(String settingName) { 62 return getFormatter().getSettingValue(settingName); 63 } 64 65 public void setValue(String settingName, Object newValue, String propertyName) { 66 Object oldValue = getValue(settingName); 67 if ((oldValue == null && newValue == null) 68 || (oldValue != null && oldValue.equals(newValue)) 69 ) { 70 return; } 72 73 getFormatter().setSettingValue(settingName, newValue); 74 75 if (propertyName != null){ 76 firePropertyChange(propertyName, oldValue, newValue); 77 } 78 } 79 80 84 public void setValue(String settingName, Object newValue) { 85 setValue(settingName, newValue, null); 86 } 87 88 89 public int indentLine(Document doc, int offset) { 90 return getFormatter().indentLine(doc, offset); 91 } 92 93 public int indentNewLine(Document doc, int offset) { 94 return getFormatter().indentNewLine(doc, offset); 95 } 96 97 public Writer createWriter(Document doc, int offset, Writer writer) { 98 return getFormatter().createWriter(doc, offset, writer); 99 } 100 101 protected boolean acceptMimeType(String mimeType) { 102 if (acceptedMimeTypes != null) { 103 for (int i = acceptedMimeTypes.length - 1; i >= 0; i--) { 104 if (acceptedMimeTypes[i].equals(mimeType)) { 105 return true; 106 } 107 } 108 } 109 110 return false; 111 } 112 113 public boolean isExpandTabs() { 114 return getFormatter().expandTabs(); 115 } 116 117 public void setExpandTabs(boolean expandTabs) { 118 boolean old = getFormatter().expandTabs(); 119 getFormatter().setExpandTabs(expandTabs); 121 if (old != expandTabs) { 122 setValue(SettingsNames.EXPAND_TABS, 123 Boolean.valueOf(expandTabs), EXPAND_TABS_PROP); 124 125 firePropertyChange(EXPAND_TABS_PROP, 126 old ? Boolean.TRUE : Boolean.FALSE, 127 expandTabs ? Boolean.TRUE : Boolean.FALSE 128 ); 129 } 130 131 } 132 133 public int getSpacesPerTab() { 134 return getFormatter().getSpacesPerTab(); 135 } 136 137 public void setSpacesPerTab(int spacesPerTab) { 138 if (spacesPerTab <= 0) { 139 NbEditorUtilities.invalidArgument("MSG_NegativeValue"); return; } 142 143 int old = getFormatter().getSpacesPerTab(); 144 getFormatter().setSpacesPerTab(spacesPerTab); 145 if (old != spacesPerTab) { 146 setValue(SettingsNames.SPACES_PER_TAB, 147 new Integer (spacesPerTab), SPACES_PER_TAB_PROP); 148 149 firePropertyChange(SPACES_PER_TAB_PROP, 150 new Integer (old), 151 new Integer (spacesPerTab) 152 ); 153 } 154 } 155 156 public void setAcceptedMimeTypes(String [] mimes) { 157 this.acceptedMimeTypes = mimes; 158 } 159 160 public String [] getAcceptedMimeTypes() { 161 return acceptedMimeTypes; 162 } 163 164 165 166 168 private static final ObjectStreamField[] serialPersistentFields = { 169 new ObjectStreamField(EXPAND_TABS_PROP, Boolean.TYPE), 170 new ObjectStreamField(SPACES_PER_TAB_PROP, Integer.TYPE) 171 }; 172 173 private void readObject(java.io.ObjectInputStream ois) 174 throws IOException, ClassNotFoundException { 175 ObjectInputStream.GetField fields = ois.readFields(); 176 setExpandTabs(fields.get(EXPAND_TABS_PROP, SettingsDefaults.defaultExpandTabs.booleanValue())); 177 setSpacesPerTab(fields.get(SPACES_PER_TAB_PROP, SettingsDefaults.defaultSpacesPerTab.intValue())); 178 } 179 180 private void writeObject(java.io.ObjectOutputStream oos) 181 throws IOException, ClassNotFoundException { 182 ObjectOutputStream.PutField fields = oos.putFields(); 183 fields.put(EXPAND_TABS_PROP, isExpandTabs()); 184 fields.put(SPACES_PER_TAB_PROP, getSpacesPerTab()); 185 oos.writeFields(); 186 } 187 188 } 189 190 | Popular Tags |