~cdparra/gelee/trunk

« back to all changes in this revision

Viewing changes to webui/runtime/draw2d/Button.js

  • Committer: parra
  • Date: 2010-03-15 15:56:56 UTC
  • Revision ID: svn-v4:ac5bba68-f036-4e09-846e-8f32731cc928:trunk/gelee:1448
merged gelee at svn

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* This notice must be untouched at all times.
 
2
 
 
3
Open-jACOB Draw2D
 
4
The latest version is available at
 
5
http://www.openjacob.org
 
6
 
 
7
Copyright (c) 2006 Andreas Herz. All rights reserved.
 
8
Created 5. 11. 2006 by Andreas Herz (Web: http://www.freegroup.de )
 
9
 
 
10
LICENSE: LGPL
 
11
 
 
12
This library is free software; you can redistribute it and/or
 
13
modify it under the terms of the GNU Lesser General Public
 
14
License (LGPL) as published by the Free Software Foundation; either
 
15
version 2.1 of the License, or (at your option) any later version.
 
16
 
 
17
This library is distributed in the hope that it will be useful,
 
18
but WITHOUT ANY WARRANTY; without even the implied warranty of
 
19
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
20
Lesser General Public License for more details.
 
21
 
 
22
You should have received a copy of the GNU Lesser General Public
 
23
License along with this library; if not, write to the Free Software
 
24
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA,
 
25
or see http://www.gnu.org/copyleft/lesser.html
 
26
*/
 
27
 
 
28
/**
 
29
 * 
 
30
 * @version 0.9.14
 
31
 * @author Andreas Herz
 
32
 * @constructor
 
33
 */
 
34
draw2d.Button=function(/*:draw2d.PaletteWindow*/ palette, /*:int*/ width, /*:int*/ height)
 
35
{
 
36
  /** @private **/
 
37
  this.x   = 0;
 
38
  /** @private **/
 
39
  this.y   = 0;
 
40
  /** @private **/
 
41
  this.id  = this.generateUId();
 
42
  /** @private **/
 
43
  this.enabled=true;
 
44
  /** @private **/
 
45
  this.active=false;
 
46
  /** @private **/
 
47
  this.palette = palette;
 
48
 
 
49
  if(width && height)
 
50
    this.setDimension(width,height);
 
51
  else
 
52
    this.setDimension(24,24);
 
53
 
 
54
  /** @private **/
 
55
  this.html = this.createHTMLElement();
 
56
}
 
57
 
 
58
draw2d.Button.prototype.type="Button";
 
59
 
 
60
/**
 
61
 * @private
 
62
 **/
 
63
draw2d.Button.prototype.dispose=function()
 
64
{
 
65
  //this.id   = null; don't dispose the id! This is important for deregistration
 
66
  //this.html = null; don't dispose the id! This is important for deregistration
 
67
}
 
68
 
 
69
 
 
70
draw2d.Button.prototype.getImageUrl=function()
 
71
{
 
72
  if(this.enabled)
 
73
    return this.type+".png";
 
74
  else
 
75
    return this.type+"_disabled.png";
 
76
}
 
77
 
 
78
 
 
79
/**
 
80
 * @private
 
81
 **/
 
82
draw2d.Button.prototype.createHTMLElement=function()
 
83
{
 
84
    var item = document.createElement('div');
 
85
    item.id        = this.id;
 
86
    item.style.position="absolute";
 
87
    item.style.left   = this.x+"px";
 
88
    item.style.top    = this.y+"px";
 
89
    item.style.height = this.width+"px";
 
90
    item.style.width  = this.height+"px";
 
91
    item.style.margin = "0px";
 
92
    item.style.padding= "0px";
 
93
    item.style.outline= "none";
 
94
 
 
95
    if(this.getImageUrl()!=null)
 
96
      item.style.backgroundImage="url("+this.getImageUrl()+")";
 
97
    else
 
98
      item.style.backgroundImage="";
 
99
 
 
100
    var oThis = this;
 
101
    this.omousedown=function(event)
 
102
    {
 
103
       if(oThis.enabled)
 
104
       {
 
105
          oThis.setActive(true);
 
106
       }
 
107
       event.cancelBubble = true;
 
108
       event.returnValue = false;
 
109
    }
 
110
    this.omouseup=function(event)
 
111
    {
 
112
       if(oThis.enabled)
 
113
       {
 
114
          oThis.setActive(false);
 
115
          oThis.execute();
 
116
       }
 
117
       event.cancelBubble = true;
 
118
       event.returnValue = false;
 
119
    }
 
120
 
 
121
    if (item.addEventListener) 
 
122
    {
 
123
      item.addEventListener("mousedown", this.omousedown, false);
 
124
      item.addEventListener("mouseup", this.omouseup, false);
 
125
    }
 
126
    else if (item.attachEvent) 
 
127
    {
 
128
      item.attachEvent("onmousedown", this.omousedown);
 
129
      item.attachEvent("onmouseup", this.omouseup);
 
130
    }
 
131
 
 
132
    return item;
 
133
}
 
134
 
 
135
/**
 
136
 * @private
 
137
 **/
 
138
draw2d.Button.prototype.getHTMLElement=function()
 
139
{
 
140
  if(this.html==null)
 
141
    this.html = this.createHTMLElement();
 
142
  return this.html;
 
143
}
 
144
 
 
145
 
 
146
/**
 
147
 *
 
148
 **/
 
149
draw2d.Button.prototype.execute=function()
 
150
{
 
151
}
 
152
 
 
153
draw2d.Button.prototype.setTooltip=function(/*:String*/ tooltipText)
 
154
{
 
155
  this.tooltip = tooltipText;
 
156
  if(this.tooltip!=null)
 
157
     this.html.title=this.tooltip;
 
158
  else
 
159
     this.html.title="";
 
160
 
 
161
}
 
162
 
 
163
/**
 
164
 **/
 
165
draw2d.Button.prototype.setActive=function(flag /*:boolean*/)
 
166
{
 
167
  if(!this.enabled)
 
168
    return;
 
169
  this.active = flag;
 
170
  if(flag==true)
 
171
    this.html.style.border="2px inset";
 
172
  else
 
173
    this.html.style.border="0px";
 
174
}
 
175
 
 
176
draw2d.Button.prototype.isActive=function()
 
177
{
 
178
  return this.active;
 
179
}
 
180
 
 
181
/**
 
182
 * @private
 
183
 **/
 
184
draw2d.Button.prototype.setEnabled=function(flag /*:boolean*/)
 
185
{
 
186
  this.enabled=flag;
 
187
  if(this.getImageUrl()!=null)
 
188
    this.html.style.backgroundImage="url("+this.getImageUrl()+")";
 
189
  else
 
190
    this.html.style.backgroundImage="";
 
191
}
 
192
 
 
193
/**
 
194
 *
 
195
 **/
 
196
draw2d.Button.prototype.setDimension=function( w /*:int*/, h /*:int*/)
 
197
{
 
198
  this.width = w;
 
199
  this.height= h;
 
200
 
 
201
  // Falls das Element noch nie gezeichnet wurde, dann braucht aus das HTML nicht 
 
202
  // aktualisiert werden
 
203
  //
 
204
  if(this.html==null)
 
205
    return;
 
206
 
 
207
  this.html.style.width  = this.width+"px";
 
208
  this.html.style.height = this.height+"px";
 
209
}
 
210
 
 
211
/**
 
212
 *
 
213
 **/
 
214
draw2d.Button.prototype.setPosition=function(/*:int*/ xPos ,/*:int*/ yPos)
 
215
{
 
216
  this.x = Math.max(0,xPos);
 
217
  this.y = Math.max(0,yPos);
 
218
  // Falls das Element noch nie gezeichnet wurde, dann braucht aus das HTML nicht 
 
219
  // aktualisiert werden
 
220
  //
 
221
  if(this.html==null)
 
222
    return;
 
223
 
 
224
  this.html.style.left = this.x+"px";
 
225
  this.html.style.top  = this.y+"px";
 
226
}
 
227
 
 
228
/**
 
229
 *
 
230
 **/
 
231
draw2d.Button.prototype.getWidth=function()
 
232
{
 
233
  return this.width;
 
234
}
 
235
 
 
236
/**
 
237
 *
 
238
 **/
 
239
draw2d.Button.prototype.getHeight=function()
 
240
{
 
241
  return this.height;
 
242
}
 
243
 
 
244
/**
 
245
 *
 
246
 **/
 
247
draw2d.Button.prototype.getY=function()
 
248
{
 
249
    return this.y;
 
250
}
 
251
 
 
252
/**
 
253
 *
 
254
 **/
 
255
draw2d.Button.prototype.getX=function()
 
256
{
 
257
    return this.x;
 
258
}
 
259
 
 
260
/**
 
261
 *
 
262
 **/
 
263
draw2d.Button.prototype.getPosition=function()
 
264
{
 
265
  return new draw2d.Point(this.x, this.y);
 
266
}
 
267
 
 
268
/**
 
269
 * @type ToolPalette
 
270
 **/
 
271
draw2d.Button.prototype.getToolPalette=function()
 
272
{
 
273
  return this.palette;
 
274
}
 
275
 
 
276
 
 
277
 
 
278
/**
 
279
 * @private
 
280
 * @returns String
 
281
 **/
 
282
draw2d.Button.prototype.generateUId=function() 
 
283
{
 
284
  var chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXTZabcdefghiklmnopqrstuvwxyz";
 
285
  var string_length = 10;
 
286
  var maxTry = 10;
 
287
  nbTry = 0
 
288
  while (nbTry < 1000) 
 
289
  {
 
290
      var id = '';
 
291
      // generate string
 
292
      for (var i=0; i<string_length; i++) 
 
293
      {
 
294
          var rnum = Math.floor(Math.random() * chars.length);
 
295
          id += chars.substring(rnum,rnum+1);
 
296
      }
 
297
      // check if there
 
298
      elem = document.getElementById(id);
 
299
      if (!elem)
 
300
          return id
 
301
      nbTry += 1
 
302
  }
 
303
  return null
 
304
}