File: MQScriptController.m

package info (click to toggle)
mysql-gui-tools 5.0r14%2BopenSUSE-2.1
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 116,956 kB
  • ctags: 48,715
  • sloc: sql: 341,918; pascal: 276,698; ansic: 91,020; cpp: 90,451; objc: 33,236; sh: 29,481; yacc: 10,756; xml: 10,589; java: 10,079; php: 2,806; python: 2,092; makefile: 1,783; perl: 4
file content (421 lines) | stat: -rw-r--r-- 12,372 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
//
//  MQScriptController.m
//  MySQL QueryBrowser
//
//  Created by Alfredo Kojima on 5/31/05.
//  Copyright 2005 MySQL AB. All rights reserved.
//

#import "MQueryBrowserDocument.h"
#import "MQScriptController.h"
#import "MQueryBrowser.h"
#import "MQScriptTab.h"
#import "MQScriptExecutionHelper.h"
#import <MySQLToolsCommon/MMenuButton.h>

@implementation MQScriptController

- (void)awakeFromNib
{
  NSRect wrect= [[owner window] contentRectForFrameRect:[[owner window] frame]];
  NSRect trect;
  trect= [toolbar frame];
  [toolbar setFrameOrigin:NSMakePoint(-1,NSHeight(wrect)-NSHeight(trect))];

  trect= [bigToolbar frame];
  [bigToolbar setFrameOrigin:NSMakePoint(-1,NSHeight(wrect)-NSHeight(trect))];
  
  _idleImage= [[NSImage imageNamed:@"sakila.png"] retain];
  _busyImage= [[NSImage imageNamed:@"dolphin_anim.gif"] retain];
  
  [[NSNotificationCenter defaultCenter] addObserver:self
                                           selector:@selector(scriptEditChanged:)
                                               name:MQScriptEditStatusChangedNotification
                                             object:nil];
}


- (void)dealloc
{
  [[NSNotificationCenter defaultCenter] removeObserver:self];
  [_idleImage release];
  [_busyImage release];
  
  [super dealloc];
}


- (MQScriptTab*)createPage
{
  MQScriptTab *page= [[MQScriptTab alloc] initWithIdentifier:@"bb" 
                                            withConnectionTo:[[owner document] serverInfo]];
  if (page)
  {
    [page setIcon:[NSImage imageNamed:@"tabsheet_icon_script.png"]];
    [page setLabel:@"Script"];
    [page setDefaultSchema:[[owner document] defaultSchema]];
    [(id)[[page scriptEditor] textView] setHelper:[owner schemaHelper]];

    [[page scriptHelper] setDelegate:self];
  }
  return [page autorelease];
}



- (void)switchToolbar:(BOOL)small
{
  if ([[owner currentTab] isKindOfClass:[MQScriptTab class]])
  {
    [bigToolbar setHidden:small?YES:NO];
    [toolbar setHidden:small?NO:YES];
  }
}


- (void)resizeToolbar:(float)height
{
  [bigToolbar setFrameSize:NSMakeSize(NSWidth([bigToolbar frame]), height)];
}


- (void)updateToolbarState
{
  MQScriptTab *page= [owner currentTab];
  MQScriptState state= [[page scriptHelper] state];

  switch (state)
  {
    case ScriptBreakpoint:
    case ScriptWaiting:
      switch (state)
      {
        case ScriptBreakpoint:
          [page setStatusText:@"Breakpoint reached." icon:nil];
          break;
        default:      
          [page setStatusText:@"Press Step or Continue." icon:nil];
          break;
      }
      [executeButton setEnabled:NO];
      [[toolbar viewWithTag:24] setEnabled:NO];
      [stopButton setEnabled:YES];
      [[toolbar viewWithTag:25] setEnabled:YES];
      [pauseButton setEnabled:NO];
      [[toolbar viewWithTag:26] setEnabled:NO];
      [continueButton setEnabled:YES];
      [[toolbar viewWithTag:27] setEnabled:YES];
      [stepButton setEnabled:YES];
      [[toolbar viewWithTag:28] setEnabled:YES];
      [animbox setImage:_idleImage];
      break;
    case ScriptError:
      [executeButton setEnabled:NO];
      [[toolbar viewWithTag:24] setEnabled:NO];
      [stopButton setEnabled:YES];
      [[toolbar viewWithTag:25] setEnabled:YES];
      [pauseButton setEnabled:NO];
      [[toolbar viewWithTag:26] setEnabled:NO];
      [continueButton setEnabled:YES];
      [[toolbar viewWithTag:27] setEnabled:YES];
      [stepButton setEnabled:YES];
      [[toolbar viewWithTag:28] setEnabled:YES];
      [animbox setImage:_idleImage];
      break;      
    case ScriptExecuting:
      [executeButton setEnabled:NO];
      [[toolbar viewWithTag:24] setEnabled:NO];
      [stopButton setEnabled:YES];
      [[toolbar viewWithTag:25] setEnabled:YES];
      [pauseButton setEnabled:YES];
      [[toolbar viewWithTag:26] setEnabled:YES];
      [continueButton setEnabled:NO];
      [[toolbar viewWithTag:27] setEnabled:NO];
      [stepButton setEnabled:NO];
      [[toolbar viewWithTag:28] setEnabled:NO];
      [animbox setImage:_busyImage];
      [page setStatusText:@"Executing..." icon:nil];
      break;
    case ScriptFinished:
    case ScriptStopped:
      if (state == ScriptFinished)
        [page setStatusText:@"Script executed." icon:nil];
      else
        [page setStatusText:@"Script stopped." icon:nil];
    case ScriptIdle:
      [executeButton setEnabled:YES];
      [[toolbar viewWithTag:24] setEnabled:YES];
      [stopButton setEnabled:NO];
      [[toolbar viewWithTag:25] setEnabled:NO];
      [pauseButton setEnabled:NO];
      [[toolbar viewWithTag:26] setEnabled:NO];
      [continueButton setEnabled:NO];
      [[toolbar viewWithTag:27] setEnabled:NO];
      [stepButton setEnabled:NO];
      [[toolbar viewWithTag:28] setEnabled:NO];
      [animbox setImage:_idleImage];
      break;
  }
}


- (IBAction)saveScript:(id)sender
{
  [[owner currentTab] saveScript:sender];
}


- (void)openScript
{
  NSOpenPanel *panel= [NSOpenPanel openPanel];
  NSView *group= [[[NSView alloc] initWithFrame:NSMakeRect(0, 0, 235, 26)] autorelease];
  NSPopUpButton *popup= [[[NSPopUpButton alloc] initWithFrame:NSMakeRect(100,0,130,26)] autorelease];
  NSTextField *caption= [[[NSTextField alloc] initWithFrame:NSMakeRect(0,4,100,17)] autorelease];
  static struct {
    NSString *name;
    NSStringEncoding encoding;
  } encodings[]= {
  { @"UTF-8",      NSUTF8StringEncoding},
  { @"Latin1",     NSISOLatin1StringEncoding},
  { @"Latin2",     NSISOLatin2StringEncoding},
  { @"ISO 2022JP", NSISO2022JPStringEncoding},
  { @"EUC-JP",     NSJapaneseEUCStringEncoding},
  { @"Shift-JIS",  NSShiftJISStringEncoding},
  { @"MacRoman",   NSMacOSRomanStringEncoding},
  { @"CP1250",     NSWindowsCP1250StringEncoding},
  { @"CP1251",     NSWindowsCP1251StringEncoding},
  { @"CP1252",     NSWindowsCP1252StringEncoding},
  { @"CP1253",     NSWindowsCP1253StringEncoding},
  { @"CP1254",     NSWindowsCP1254StringEncoding},
  };
  unsigned int i;
  for (i= 0; i < sizeof(encodings)/sizeof(*encodings); i++)
    [popup addItemWithTitle:encodings[i].name];
  
  [caption setBordered:NO];
  [caption setStringValue:@"Text Encoding:"];
  [caption setEditable:NO];
  [caption setDrawsBackground:NO];
  [caption setAutoresizingMask:NSViewMinXMargin];
  [popup setAutoresizingMask:NSViewWidthSizable];
  [group addSubview:caption];
  [group addSubview:popup];
  
  [panel setAccessoryView:group];
  [panel setTitle:@"Open SQL Script"];
  
  if ([panel runModalForTypes:[NSArray arrayWithObjects:@"txt", @"sql", @"", nil]] == NSFileHandlingPanelOKButton)
  {
    NSData *data;
    NSString *text;
    NSString *filename= [panel filename];
        
    data= [[NSData alloc] initWithContentsOfFile:filename];
    if (!data)
    {
      NSRunAlertPanel(@"Error",
                      [NSString stringWithFormat:@"Could not open file '%@'.", filename],
                      @"OK",nil,nil);
      return;
    }
    
    if (!(text=[[NSString alloc] initWithData:data encoding:encodings[[popup indexOfSelectedItem]].encoding]))
    {
      NSRunAlertPanel(@"Error",
                      [NSString stringWithFormat:@"Could not convert file data to %@ encoding.", [popup titleOfSelectedItem]],
                      @"OK",nil,nil);
      [data release];
      return;
    }
    [data release];

    [(MQScriptTab*)[owner currentTab] setScript:text];
    [(MQScriptTab*)[owner currentTab] setDocumentEdited:NO];
    [(MQScriptTab*)[owner currentTab] setFilename:filename];
    
    [text release];
  }
}


- (void)openScriptDidEnd:(NSAlert*)alert returnCode:(int)returnCode contextInfo:(void*)info
{
  MQScriptTab *page= (MQScriptTab*)info;
  
  switch (returnCode)
  {
    case NSAlertDefaultReturn:
      [page saveScript:nil];
      if ([page isDocumentEdited]) // this means the Save was cancelled
        return;
      break;
    case NSAlertAlternateReturn:
      break;
    case NSAlertOtherReturn:
      return;
  }
  [self openScript];
}


- (IBAction)openScriptInCurrentEditor:(id)sender
{
  MQScriptTab *page= (MQScriptTab*)[owner currentTab];
  
  if ([page isDocumentEdited])
  {
    NSAlert *alert= [NSAlert alertWithMessageText:@"Save script before loading a new one in its place?"
                                    defaultButton:@"Save"
                                  alternateButton:@"Don't Save"
                                      otherButton:@"Cancel"
                        informativeTextWithFormat:@"The current tab sheet contains changes that will be lost if you do not save."];

    [alert beginSheetModalForWindow:[owner window]
                      modalDelegate:self
                     didEndSelector:@selector(openScriptDidEnd:returnCode:contextInfo:)
                        contextInfo:page];
  }
  else
    [self openScript];
}


- (IBAction)openScript:(id)sender
{
  [self setupEditorWithScript:@""];
  [self openScript];
}


- (IBAction)executeScript:(id)sender
{
  [[(MQScriptTab*)[owner currentTab] scriptHelper] executeScript:sender];
}

- (IBAction)stopScript:(id)sender
{
  [[(MQScriptTab*)[owner currentTab] scriptHelper] stopScript:sender];
}

- (IBAction)continueScript:(id)sender
{
  [[(MQScriptTab*)[owner currentTab] scriptHelper] continueScript:sender];
}

- (IBAction)pauseScript:(id)sender
{
  [[(MQScriptTab*)[owner currentTab] scriptHelper] pauseScript:sender];
}

- (IBAction)stepNextScript:(id)sender
{
  [[(MQScriptTab*)[owner currentTab] scriptHelper] stepScript:sender];
}

- (void)willShowPage:(id)page
{
  [animbox setImage:_idleImage];

  [bigToolbar setHidden:[owner smallToolbar]?YES:NO];
  [toolbar setHidden:[owner smallToolbar]?NO:YES];
  
  [owner setDocumentEdited:[page isDocumentEdited]];
  //XXX
//  [owner setTitleForDocumentName:[NSString stringWithFormat:[NSString stringWithUTF8String:"%@ — %@"],
//                                       [page documentName]?:@"Untitled", [page defaultSchema]]];
  
  [self updateToolbarState];

  // somehow, the dragged types is being lost when tabs are switched
  if ([[owner currentTab] isKindOfClass:[MQScriptTab class]])
    [[[[owner currentTab] scriptEditor] textView] registerForDraggedTypes:[NSArray arrayWithObject:MSchemaItemPboardType]];
}

- (void)didHidePage
{
  [toolbar setHidden:YES];
  [bigToolbar setHidden:YES];
}


- (void)setupEditorWithScript:(NSString*)script
{
  MQScriptTab *page;
  
  if (![[owner currentTab] isKindOfClass:[MQScriptTab class]]
      || [[[owner currentTab] script] length] > 0)
    [owner addScriptTab:nil];

  page= [owner currentTab];
  
  [page setScript:script];
}


- (void)scriptEditChanged:(NSNotification*)notif
{
  MQScriptTab *tab= [notif object];
  if (tab == [owner currentTab])
  {
    [owner setDocumentEdited:[tab isDocumentEdited]];
    
  //XXX  
//    [owner setTitleForDocumentName:[NSString stringWithFormat:[NSString stringWithUTF8String:"%@ — %@"],
//                                          [tab documentName]?:@"Untitled", [tab defaultSchema]]];
  }
}


- (void)scriptChangedState:(MQScriptExecutionHelper*)script state:(MQScriptState)state
{
  if ([[owner currentTab] scriptHelper] == script)
    [self updateToolbarState];
}


- (BOOL)stateOfMenuItem:(NSMenuItem*)item atIndex:(int)index inMenu:(NSMenu*)menu
{
  if ([[[NSApp mainMenu] itemAtIndex:[[NSApp mainMenu] indexOfItemWithSubmenu:menu]] tag]== 106)
  {
    MQScriptTab *page= [owner currentTab];
    MQScriptState state;
    BOOL flag= NO;
    
    if (![page isKindOfClass:[MQScriptTab class]])
    {
      [item setEnabled:NO];
      return YES;
    }
    state= [[page scriptHelper] state];

    switch (index)
    {
      case 0: // Execute Script
      case 1: // Execute Selection
      case 2: // Execute Stepping
        if (state == ScriptFinished || state == ScriptStopped || state == ScriptIdle)
          flag= YES;
        break;
      case 4: // Pause
        if (state == ScriptExecuting)
          flag= YES;
        break;
      case 5: // Continue
        if (state == ScriptError || state == ScriptBreakpoint || state == ScriptWaiting)
          flag= YES;
        break;
      case 6: // Step
        if (state == ScriptError || state == ScriptBreakpoint || state == ScriptWaiting)
          flag= YES;
        break;
      case 8: // Stop
        if (state != ScriptFinished && state != ScriptStopped && state != ScriptIdle)
          flag= YES;
        break;
    }
    [item setEnabled:flag];
  }
  return YES;
}

@end