OpenASIP  2.0
OSEdModifyBehaviorCmd.cc
Go to the documentation of this file.
1 /*
2  Copyright (c) 2002-2009 Tampere University.
3 
4  This file is part of TTA-Based Codesign Environment (TCE).
5 
6  Permission is hereby granted, free of charge, to any person obtaining a
7  copy of this software and associated documentation files (the "Software"),
8  to deal in the Software without restriction, including without limitation
9  the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  and/or sell copies of the Software, and to permit persons to whom the
11  Software is furnished to do so, subject to the following conditions:
12 
13  The above copyright notice and this permission notice shall be included in
14  all copies or substantial portions of the Software.
15 
16  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19  THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22  DEALINGS IN THE SOFTWARE.
23  */
24 /**
25  * @file OSEdModifyBehaviorCmd.cc
26  *
27  * Definition of OSEdModifyBehaviorCmd class.
28  *
29  * @author Jussi Nykänen 2004 (nykanen-no.spam-cs.tut.fi)
30  * @note rating: red
31  */
32 
33 #include <boost/format.hpp>
34 
35 #include "OSEdModifyBehaviorCmd.hh"
36 #include "OSEdConstants.hh"
37 #include "OperationContainer.hh"
38 #include "FileSystem.hh"
39 #include "OSEd.hh"
40 #include "WxConversion.hh"
41 #include "ErrorDialog.hh"
42 #include "OSEdTextGenerator.hh"
43 #include "CommandThread.hh"
44 #include "OperationModule.hh"
45 #include "OSEdTreeView.hh"
46 #include "OSEdOptions.hh"
47 
48 using std::string;
49 using boost::format;
50 
51 /**
52  * Constructor.
53  */
55  GUICommand(OSEdConstants::CMD_NAME_MODIFY_BEHAVIOR, NULL) {
56 }
57 
58 /**
59  * Destructor.
60  */
62 }
63 
64 /**
65  * Returns the id of the command.
66  *
67  * @return The id of the command.
68  */
69 int
72 }
73 
74 /**
75  * Creates a new command.
76  *
77  * @return A new command.
78  */
81  return new OSEdModifyBehaviorCmd();
82 }
83 
84 /**
85  * Executes the command.
86  *
87  * @return True if execution is successful.
88  */
89 bool
91 
93  OSEdOptions* options = wxGetApp().options();
94  string editor = options->editor();
95  if (editor == "") {
96  format fmt = texts.text(OSEdTextGenerator::TXT_ERROR_NO_EDITOR);
98  error.ShowModal();
99  return false;
100  } else if (!FileSystem::fileExists(editor)) {
101  format fmt = texts.text(OSEdTextGenerator::TXT_ERROR_OPEN);
102  fmt % editor;
104  error.ShowModal();
105  return false ;
106  } else {
107 
108  OSEdTreeView* treeView = wxGetApp().mainFrame()->treeView();
109  wxTreeItemId opId = treeView->selectedOperationId();
110  string modName = treeView->moduleOfOperation(opId);
111  wxTreeItemId modId = treeView->moduleIdOfOperation(opId);
112  string pathName = treeView->pathOfModule(modId);
113 
114  OperationModule& module = OperationContainer::module(pathName, modName);
115  string cmd = "";
116  if (module.hasBehaviorSource()) {
117  string code = module.behaviorSourceModule();
118  cmd = editor + " " + code;
119  } else {
120  // new behavior is added for the module
121  string code = module.propertiesModule();
122  size_t pos = code.find_last_of(".");
123  code.erase(pos);
124  code += ".cc";
125 
126  // copy template file as new file
127  string dir = Environment::dataDirPath(
129 
130  string templateFile = dir + FileSystem::DIRECTORY_SEPARATOR +
132 
133  FileSystem::copy(templateFile, code);
134  cmd = editor + " " + code;
135  }
136  // we are using detached thread that will be automatically cleaned
137  CommandThread* thread = new CommandThread(cmd);
138  thread->Create();
139  thread->Run();
140  return true;
141  }
142 
143  assert(false);
144  return false;
145 }
146 
147 /**
148  * Returns true if command is enabled.
149  *
150  * Command is enable when path of the selected operation is writable.
151  *
152  * @return True if command is enabled.
153  */
154 bool
156  OSEdTreeView* treeView = wxGetApp().mainFrame()->treeView();
157  if (treeView->isOperationSelected()) {
158  wxTreeItemId opId = treeView->selectedOperationId();
159  string mod = treeView->moduleOfOperation(opId);
160  wxTreeItemId modId = treeView->moduleIdOfOperation(opId);
161  string path = treeView->pathOfModule(modId);
162 
163  if (FileSystem::fileIsWritable(path)) {
164  return true;
165  } else {
166  return false;
167  }
168  } else {
169  return false;
170  }
171 }
172 
173 /**
174  * Returns the icon path.
175  *
176  * @return Empty string (icons not used).
177  */
178 string
180  return "";
181 }
OSEdConstants::BEHAVIOR_TEMPLATE_FILE_NAME
static const std::string BEHAVIOR_TEMPLATE_FILE_NAME
Name of the behavior template file name.
Definition: OSEdConstants.hh:82
OSEdConstants::APPLICATION_NAME
static const wxString APPLICATION_NAME
The name of the application.
Definition: OSEdConstants.hh:110
FileSystem.hh
WxConversion::toWxString
static wxString toWxString(const std::string &source)
OSEdTreeView::pathOfModule
std::string pathOfModule(wxTreeItemId id)
Definition: OSEdTreeView.cc:431
CommandThread.hh
OSEdModifyBehaviorCmd::OSEdModifyBehaviorCmd
OSEdModifyBehaviorCmd()
Definition: OSEdModifyBehaviorCmd.cc:54
OSEdTreeView::moduleIdOfOperation
wxTreeItemId moduleIdOfOperation(wxTreeItemId id)
Definition: OSEdTreeView.cc:420
OperationContainer::module
static OperationModule & module(const std::string &path, const std::string &mod)
Definition: OperationContainer.cc:148
OSEdModifyBehaviorCmd.hh
OSEd.hh
Texts::TextGenerator::text
virtual boost::format text(int textId)
Definition: TextGenerator.cc:94
OSEdConstants.hh
assert
#define assert(condition)
Definition: Application.hh:86
OperationModule::propertiesModule
virtual std::string propertiesModule() const
Definition: OperationModule.cc:121
GUICommand
Definition: GUICommand.hh:43
ErrorDialog
Definition: ErrorDialog.hh:42
FileSystem::fileIsWritable
static bool fileIsWritable(const std::string fileName)
OSEdTreeView
Definition: OSEdTreeView.hh:54
ErrorDialog.hh
OperationModule::behaviorSourceModule
virtual std::string behaviorSourceModule() const
Definition: OperationModule.cc:144
OSEdTreeView::moduleOfOperation
std::string moduleOfOperation(wxTreeItemId id)
Definition: OSEdTreeView.cc:407
OSEdConstants
Definition: OSEdConstants.hh:45
OSEdTreeView::isOperationSelected
bool isOperationSelected() const
Definition: OSEdTreeView.cc:745
OSEdTextGenerator::TXT_ERROR_NO_EDITOR
@ TXT_ERROR_NO_EDITOR
Error when no editor is given.
Definition: OSEdTextGenerator.hh:140
OSEdModifyBehaviorCmd::icon
virtual std::string icon() const
Definition: OSEdModifyBehaviorCmd.cc:179
FileSystem::copy
static void copy(const std::string &source, const std::string &target)
Definition: FileSystem.cc:524
OSEdTextGenerator::TXT_ERROR_OPEN
@ TXT_ERROR_OPEN
Error when opening fails.
Definition: OSEdTextGenerator.hh:141
OSEdModifyBehaviorCmd::create
virtual GUICommand * create() const
Definition: OSEdModifyBehaviorCmd.cc:80
OSEdModifyBehaviorCmd::id
virtual int id() const
Definition: OSEdModifyBehaviorCmd.cc:70
OSEdModifyBehaviorCmd::Do
virtual bool Do()
Definition: OSEdModifyBehaviorCmd.cc:90
FileSystem::DIRECTORY_SEPARATOR
static const std::string DIRECTORY_SEPARATOR
Definition: FileSystem.hh:189
CommandThread
Definition: CommandThread.hh:43
OSEdTreeView.hh
options
static MachInfoCmdLineOptions options
Definition: MachInfo.cc:46
false
find Finds info of the inner loops in the false
Definition: InnerLoopFinder.cc:81
FileSystem::fileExists
static bool fileExists(const std::string fileName)
OperationModule
Definition: OperationModule.hh:46
OperationModule::hasBehaviorSource
virtual bool hasBehaviorSource() const
Definition: OperationModule.cc:131
OSEdTextGenerator::instance
static OSEdTextGenerator & instance()
Definition: OSEdTextGenerator.cc:214
OSEdTreeView::selectedOperationId
wxTreeItemId selectedOperationId()
Definition: OSEdTreeView.cc:379
OSEdModifyBehaviorCmd::isEnabled
virtual bool isEnabled()
Definition: OSEdModifyBehaviorCmd.cc:155
OSEdTextGenerator
Definition: OSEdTextGenerator.hh:42
WxConversion.hh
OSEdOptions.hh
OSEdOptions
Definition: OSEdOptions.hh:46
OSEdModifyBehaviorCmd::~OSEdModifyBehaviorCmd
virtual ~OSEdModifyBehaviorCmd()
Definition: OSEdModifyBehaviorCmd.cc:61
WxConversion::toString
static std::string toString(const wxString &source)
OSEdTextGenerator.hh
GUICommand::parentWindow
wxWindow * parentWindow() const
Definition: GUICommand.cc:75
OperationModule.hh
OSEdConstants::CMD_MODIFY_BEHAVIOR
@ CMD_MODIFY_BEHAVIOR
Modify operation behavior command id.
Definition: OSEdConstants.hh:97
Environment::dataDirPath
static std::string dataDirPath(const std::string &prog)
Definition: Environment.cc:176
OperationContainer.hh