~ubuntu-branches/ubuntu/quantal/netbeans/quantal

« back to all changes in this revision

Viewing changes to openide/options/src/org/openide/text/PrintSettings.java

  • Committer: Bazaar Package Importer
  • Author(s): Marek Slama
  • Date: 2008-01-29 14:11:22 UTC
  • Revision ID: james.westby@ubuntu.com-20080129141122-fnzjbo11ntghxfu7
Tags: upstream-6.0.1
ImportĀ upstreamĀ versionĀ 6.0.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 
3
 *
 
4
 * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
 
5
 *
 
6
 * The contents of this file are subject to the terms of either the GNU
 
7
 * General Public License Version 2 only ("GPL") or the Common
 
8
 * Development and Distribution License("CDDL") (collectively, the
 
9
 * "License"). You may not use this file except in compliance with the
 
10
 * License. You can obtain a copy of the License at
 
11
 * http://www.netbeans.org/cddl-gplv2.html
 
12
 * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
 
13
 * specific language governing permissions and limitations under the
 
14
 * License.  When distributing the software, include this License Header
 
15
 * Notice in each file and include the License file at
 
16
 * nbbuild/licenses/CDDL-GPL-2-CP.  Sun designates this
 
17
 * particular file as subject to the "Classpath" exception as provided
 
18
 * by Sun in the GPL Version 2 section of the License file that
 
19
 * accompanied this code. If applicable, add the following below the
 
20
 * License Header, with the fields enclosed by brackets [] replaced by
 
21
 * your own identifying information:
 
22
 * "Portions Copyrighted [year] [name of copyright owner]"
 
23
 *
 
24
 * Contributor(s):
 
25
 *
 
26
 * The Original Software is NetBeans. The Initial Developer of the Original
 
27
 * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
 
28
 * Microsystems, Inc. All Rights Reserved.
 
29
 *
 
30
 * If you wish your version of this file to be governed by only the CDDL
 
31
 * or only the GPL Version 2, indicate your decision by adding
 
32
 * "[Contributor] elects to include this software in this distribution
 
33
 * under the [CDDL or GPL Version 2] license." If you do not indicate a
 
34
 * single choice of license, a recipient has the option to distribute
 
35
 * your version of this file under either the CDDL, the GPL Version 2 or
 
36
 * to extend the choice of license to its licensees as provided above.
 
37
 * However, if you add GPL Version 2 code and therefore, elected the GPL
 
38
 * Version 2 license, then the option applies only if the new code is
 
39
 * made subject to such option by the copyright holder.
 
40
 */
 
41
package org.openide.text;
 
42
 
 
43
import org.openide.options.ContextSystemOption;
 
44
import org.openide.util.HelpCtx;
 
45
import org.openide.util.NbBundle;
 
46
import java.awt.Font;
 
47
import java.awt.print.PageFormat;
 
48
import java.awt.print.PrinterJob;
 
49
import java.io.IOException;
 
50
import java.io.ObjectInput;
 
51
import java.io.ObjectOutput;
 
52
 
 
53
/** Settings for output window.
 
54
*
 
55
* @author Ales Novak
 
56
*/
 
57
public final class PrintSettings extends ContextSystemOption {
 
58
    // final because overrides Externalizable methods
 
59
 
 
60
    /** serialVersionUID */
 
61
    static final long serialVersionUID = -9102470021814206818L;
 
62
 
 
63
    /** Constant for center position of page header. */
 
64
    public static final int CENTER = 0x1;
 
65
 
 
66
    /** Constant for right position of page header. */
 
67
    public static final int RIGHT = 0x2;
 
68
 
 
69
    /** Constant for left position of page header. */
 
70
    public static final int LEFT = 0x0;
 
71
 
 
72
    /** Property name of the wrap property */
 
73
    public static final String PROP_PAGE_FORMAT = "pageFormat"; // NOI18N
 
74
 
 
75
    /** Property name of the wrap property */
 
76
    public static final String PROP_WRAP = "wrap"; // NOI18N
 
77
 
 
78
    /** Property name of the header format  property */
 
79
    public static final String PROP_HEADER_FORMAT = "headerFormat"; // NOI18N
 
80
 
 
81
    /** Property name of the footer format property */
 
82
    public static final String PROP_FOOTER_FORMAT = "footerFormat"; // NOI18N
 
83
 
 
84
    /** Property name of the header font property */
 
85
    public static final String PROP_HEADER_FONT = "headerFont"; // NOI18N
 
86
 
 
87
    /** Property name of the footer font property */
 
88
    public static final String PROP_FOOTER_FONT = "footerFont"; // NOI18N
 
89
 
 
90
    /** Property name of the header alignment property */
 
91
    public static final String PROP_HEADER_ALIGNMENT = "headerAlignment"; // NOI18N
 
92
 
 
93
    /** Property name of the footer alignment property */
 
94
    public static final String PROP_FOOTER_ALIGNMENT = "footerAlignment"; // NOI18N
 
95
 
 
96
    /** Property name of the line ascent correction property */
 
97
    public static final String PROP_LINE_ASCENT_CORRECTION = "lineAscentCorrection"; // NOI18N
 
98
    private static final String HELP_ID = "editing.printing"; // !!! NOI18N
 
99
 
 
100
 
 
101
    public String displayName() {
 
102
        return NbBundle.getMessage(PrintSettings.class, "CTL_Print_settings");
 
103
    }
 
104
 
 
105
    public HelpCtx getHelpCtx() {
 
106
        return new HelpCtx(HELP_ID);
 
107
    }
 
108
 
 
109
    /** @return an instance of PageFormat
 
110
    * The returned page format is either previously set by
 
111
    * PageSetupAction or is acquired as a default PageFormat
 
112
    * from supported PrinterJob
 
113
    */
 
114
    public static PageFormat getPageFormat(PrinterJob pj) {
 
115
        return PrintPreferences.getPageFormat(pj);
 
116
    }
 
117
 
 
118
    /** @deprecated Use {@link #getPageFormat(PrinterJob)} instead. */
 
119
    public PageFormat getPageFormat() {
 
120
        return getPageFormat(PrinterJob.getPrinterJob());
 
121
    }
 
122
 
 
123
    /** sets page format */
 
124
    public void setPageFormat(PageFormat pf) {
 
125
        PrintPreferences.setPageFormat(pf);
 
126
        firePropertyChange(PROP_PAGE_FORMAT, null, pf);
 
127
    }
 
128
 
 
129
    public boolean getWrap() {
 
130
        return PrintPreferences.getWrap();
 
131
    }
 
132
 
 
133
    public void setWrap(boolean b) {
 
134
        PrintPreferences.setWrap(b);
 
135
        firePropertyChange(PROP_WRAP, (b ? Boolean.FALSE : Boolean.TRUE), (b ? Boolean.TRUE : Boolean.FALSE));
 
136
    }
 
137
 
 
138
    public String getHeaderFormat() {
 
139
        return PrintPreferences.getHeaderFormat();
 
140
    }
 
141
 
 
142
    public void setHeaderFormat(String s) {
 
143
        PrintPreferences.setHeaderFormat(s);
 
144
        firePropertyChange(PROP_HEADER_FORMAT, null, s);
 
145
    }
 
146
 
 
147
    public String getFooterFormat() {
 
148
        return PrintPreferences.getFooterFormat();
 
149
    }
 
150
 
 
151
    public void setFooterFormat(String s) {
 
152
        PrintPreferences.setFooterFormat(s);
 
153
        firePropertyChange(PROP_FOOTER_FORMAT, null, s);
 
154
    }
 
155
 
 
156
    public Font getHeaderFont() {
 
157
        return PrintPreferences.getHeaderFont();
 
158
    }
 
159
 
 
160
    public void setHeaderFont(Font f) {
 
161
        PrintPreferences.setHeaderFont(f);
 
162
        firePropertyChange(PROP_HEADER_FONT, null, f);
 
163
    }
 
164
 
 
165
    public Font getFooterFont() {
 
166
        return PrintPreferences.getFooterFont();
 
167
    }
 
168
 
 
169
    public void setFooterFont(Font f) {
 
170
        PrintPreferences.setFooterFont(f);
 
171
        firePropertyChange(PROP_FOOTER_FONT, null, f);
 
172
    }
 
173
 
 
174
    public int getHeaderAlignment() {
 
175
        return fromAlignment(PrintPreferences.getHeaderAlignment());
 
176
    }
 
177
 
 
178
    public void setHeaderAlignment(int alignment) {
 
179
        PrintPreferences.setHeaderAlignment(toAlignment(alignment));
 
180
        firePropertyChange(PROP_HEADER_ALIGNMENT, null, new Integer(alignment));
 
181
    }
 
182
    
 
183
    private PrintPreferences.Alignment toAlignment(int alignment) {
 
184
        PrintPreferences.Alignment retval = PrintPreferences.Alignment.CENTER;
 
185
        switch(alignment) {
 
186
            case CENTER:
 
187
                retval = PrintPreferences.Alignment.CENTER;
 
188
                break;
 
189
            case LEFT:
 
190
                retval = PrintPreferences.Alignment.LEFT;
 
191
                break;
 
192
            case RIGHT:
 
193
                retval = PrintPreferences.Alignment.RIGHT;
 
194
                break;                            
 
195
        }
 
196
        return retval;
 
197
    }
 
198
    
 
199
    private int fromAlignment(PrintPreferences.Alignment alignment) {
 
200
        int retval = CENTER;
 
201
        if (PrintPreferences.Alignment.CENTER.equals(alignment)) {
 
202
            retval = CENTER;
 
203
        } else if (PrintPreferences.Alignment.LEFT.equals(alignment)) {
 
204
            retval = LEFT;
 
205
        } else if (PrintPreferences.Alignment.RIGHT.equals(alignment)) {
 
206
            retval = RIGHT;
 
207
        }
 
208
        return retval;
 
209
    }
 
210
    
 
211
 
 
212
    public int getFooterAlignment() {
 
213
        return fromAlignment(PrintPreferences.getFooterAlignment());
 
214
    }
 
215
 
 
216
    public void setFooterAlignment(int alignment) {
 
217
        PrintPreferences.setFooterAlignment(toAlignment(alignment));
 
218
        firePropertyChange(PROP_FOOTER_ALIGNMENT, null, new Integer(alignment));
 
219
    }
 
220
 
 
221
    /** Getter for lineAscentCorrection property. */
 
222
    public float getLineAscentCorrection() {
 
223
        return PrintPreferences.getLineAscentCorrection();
 
224
    }
 
225
 
 
226
    /** Setter for lineAscentCorrection property.
 
227
    * @param correction the correction
 
228
    * @exception IllegalArgumentException if <tt>correction</tt> is less than 0.
 
229
    */
 
230
    public void setLineAscentCorrection(float correction) {
 
231
        PrintPreferences.setLineAscentCorrection(correction);
 
232
        firePropertyChange(PROP_LINE_ASCENT_CORRECTION, null, new Float(correction));
 
233
    }
 
234
 
 
235
    public void writeExternal(ObjectOutput obtos) throws IOException {
 
236
        // no-op, nobody should be externalizing this option, but if they do
 
237
        // just do not store anything
 
238
    }
 
239
    
 
240
    public void readExternal(ObjectInput obtis) throws IOException, ClassNotFoundException {
 
241
        // no-op, to ignore previously serialized options
 
242
    }
 
243
    
 
244
    /** Property editor for alignment properties */
 
245
    public static class AlignmentEditor extends java.beans.PropertyEditorSupport {
 
246
        private String sCENTER;
 
247
        private String sRIGHT;
 
248
        private String sLEFT;
 
249
        private String[] tags = new String[] {
 
250
                sLEFT = NbBundle.getMessage(PrintSettings.class, "CTL_LEFT"),
 
251
                sCENTER = NbBundle.getMessage(PrintSettings.class, "CTL_CENTER"),
 
252
                sRIGHT = NbBundle.getMessage(PrintSettings.class, "CTL_RIGHT")
 
253
            };
 
254
 
 
255
        public String[] getTags() {
 
256
            return tags;
 
257
        }
 
258
 
 
259
        public String getAsText() {
 
260
            return tags[((Integer) getValue()).intValue()];
 
261
        }
 
262
 
 
263
        public void setAsText(String s) {
 
264
            if (s.equals(sLEFT)) {
 
265
                setValue(new Integer(0));
 
266
            } else if (s.equals(sCENTER)) {
 
267
                setValue(new Integer(1));
 
268
            } else {
 
269
                setValue(new Integer(2));
 
270
            }
 
271
        }
 
272
    }
 
273
 
 
274
    /** Property editor for PageFormat instances */
 
275
    public static class PageFormatEditor extends java.beans.PropertyEditorSupport {
 
276
        /** No text */
 
277
        public String getAsText() {
 
278
            return null;
 
279
        }
 
280
 
 
281
        /* @return <tt>true</tt> */
 
282
        public boolean supportsCustomEditor() {
 
283
            return true;
 
284
        }
 
285
 
 
286
        /**
 
287
        * @return <tt>null</tt> Shows pageDialog, however.
 
288
        */
 
289
        public java.awt.Component getCustomEditor() {
 
290
            PageFormat pf = (PageFormat) getValue();
 
291
            PrinterJob pj = PrinterJob.getPrinterJob();
 
292
            PageFormat npf = pj.pageDialog(pf);
 
293
 
 
294
            //setValue(npf);
 
295
            ((PrintSettings)PrintSettings.findObject(PrintSettings.class)).setPageFormat((PageFormat) npf.clone());
 
296
            pj.cancel();
 
297
 
 
298
            return null;
 
299
        }
 
300
    }
 
301
}